我正在使用 John Resig 的简单 JavaScript 继承。
我知道我可以使用 this
变量在方法之间共享值:
var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
}
});
我想创建一个指向this
的指针,这样以后它就不会被覆盖,如下所示:
$('#id').click(function() {
// this now points to selector
});
如何创建可在类范围内访问的 that = this
指针?
最佳答案
您可以使用“.apply()”
var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
},
setDancing = function(isDancing) {
this.dancing = isDancing;
}
});
var p = new Person();
$('#id').click(function() {
p.setDancing.call(p, NEWVALUE);
// 'this' will point to 'p' in the function 'p.setDancing'
});
关于javascript - 如何创建指向此的类范围指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934253/