bind を使用して「this」のスコープを変更しているため、クリック関数内から「this」を使用して generateContent 関数を参照できます。残念ながら、これは「this」のスコープが変更されたため、this.id が機能しなくなったことを意味します。
この場合、クリックされたボタンのIDを取得する最良の方法は何ですか:
function help( doc ) {
buttons: function() {
$('.add-btn').click( function() {
this.generateContent( this.id ) );
}.bind(this));
},
generateContent: function ( id ){
}
}
// This function binds a function to an object's scope.
Function.prototype.bind = function(scope) {
var _function = this;
return function() {
return _function.apply(scope, arguments);
}
}