2
바인드를 사용하여 'this'의 범위를 변경하여 클릭 기능 내에서 'this'를 사용하여 generateContent 함수를 참조 할 수 있습니다. 불행히도 이것은 this.id가 더 이상 'this'의 범위가 변경됨에 따라 작동하지 않는다는 것을 의미합니다.바인드 프로토 타입을 사용하여 'this'의 범위 변경
이 경우, 클릭 된 버튼의 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);
}
}
대단히 감사합니다. 정확하게 알고 싶었습니다. Jquery 태그를 추가했습니다. – Lishamatish