2016-10-12 6 views
1
const _ = require('underscore'); 

module.exports = (() => { 
    const PANEL_HEADER_HEIGHT = 40; 

    return Frame.extend({ 

    ... 
    _handleSearch: _.debounce(ev => { 
     if (ev.keyCode !== 9) { 
     const searchValue = $(ev.target).val(); 
     this.model.filterData(searchValue); 
     } 
    }, 1000), 
    ... 
})(); 

this.model.filterData(searchValue); に、 を、に、未定義。 .filterData(searchValue)に変換しました。コンソールで。なぜ次のバックボーンビューコードで未定義のエラーですか?

+0

「this」は定義されていません。その文脈で「これ」が参照されることを期待しているのは何ですか? –

答えて

1

矢印機能を使用せずにメソッドを書き直すとうまくいくようです。

https://jsfiddle.net/CoryDanielson/2dt30fhb/

var MyView = Backbone.View.extend({ 
    name: 'MY-VIEW', 
    doStuff: _.debounce(function() { 
     alert(this.name); 
    }, 1000) 
}); 

var view = new MyView(); 
view.doStuff(); 
// This will alert 'MY-VIEW' 

バベルがES6仕様に従うようにコードをtranspileますので、それは失敗だ理由があります。ここ

あなたは矢印の機能についての詳細を読むことができますし、this値:バベルコードをtranspiles方法の違いで

module.exports = (() => { 
    // this = undefined, because module.exports is global. 
    return Frame.extend({ 
    // inherits this from above, undefined 
    _handleArrowFunction: _.debounce(ev => { 
     console.log(this); 
    }), 
    _handleRegularFunction: _.debounce(function() { 
     console.log(this); 
    }) 
    }); 
}) 

ルック

+0

あなたの答えの中にバベルの例を入れて、リンクを参考にしてください。 –

+1

申し訳ありません、私はそれをやって忙しかった –

関連する問題