这个????将实例方法用作事件处理程序的回调,将更改this从…“我的例子”到“不管是什么叫回调”..所以我的代码是这样的function MyObject() {
this.doSomething = function() {
...
}
var self = this
$('#foobar').bind('click', function(){
self.doSomethng()
// this.doSomething() would not work here
})}这是可行的,但这是最好的方法吗?我觉得很奇怪。
3 回答
MMTTMM
TA贡献1869条经验 获得超4个赞
function MyObject(){ var me = this; //Events Click = onClick; //Allows user to override onClick event with their own //Event Handlers onClick = function(args){ me.MyProperty = args; //Reference me, referencing this refers to onClick ... //Do other stuff }}
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
var functionX = function() { var self = this; var functionY = function(y) { // If we call "this" in here, we get a reference to functionY, // but if we call "self" (defined earlier), we get a reference to function X. }}
添加回答
举报
0/150
提交
取消