[英]can't access variable in another function inside object literal
I have following code of javascript
我有以下javascript
代码
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
$('.click').on('click', this.show);
},
show: function () {
console.log(this.over);
}
}
Obj.init();
When this does is when user clicks a .click
link then it triggers show
function and logs out the dom element created in init
function. 当这是当用户单击.click
链接时,它会触发show
function并注销在init
函数中创建的dom元素。 But the problem is then it logs out undefined. 但问题是它注销未定义。 Why? 为什么? How to solve it? 怎么解决?
try this : 试试这个 :
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
$('.click').on('click', this.show);
},
show: function () {
// here the 'this' is the button , not the obj object ..
console.log($('.over'));
}
}
Obj.init();
another option : 另外一个选项 :
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
var that = this;
$('.click').on('click', function(e){
that.show.call(that, e); // calling the show function with call, causing 'this' to be obj
});
},
// 'this' is the obj
show: function (e) {
console.log(this.over);
}
}
Obj.init();
The problem here is the scope of that this
( Obj
). 这里的问题是this
( Obj
)的范围。
Use the following code to solve your problem. 使用以下代码解决您的问题。
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
$('.click').on('click', $.proxy(this.show, this));
},
show: function () {
console.log(this.over);
}
};
Obj.init();
learn more about jQuery.proxy 了解有关jQuery.proxy的更多信息
Because jQuery injects the DOM element that was clicked on into 'this' as opposed to the 'Obj' object. 因为jQuery将点击的DOM元素注入'this'而不是'Obj'对象。 One solution is closure: 一个解决方案是关闭:
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
$('.click').on('click', this.show());
},
show: function () {
var self = this;
return function () {
console.log("over:", self.over);
}
}
}
Obj.init();
You pass the function stored in this.show
to on
. 您将存储在this.show
的函数this.show
给on
。 When it gets called, it is not called in the context of Obj
so this
is not Obj
. 当它被调用时,它不会在Obj
的上下文中调用,所以this
不是Obj
。
You need to create a new function which isn't dependent on being called in the context of Obj
. 您需要创建一个不依赖于在Obj
上下文中调用的新函数。
The easiest way to do this is with bind
: 最简单的方法是使用bind
:
$('.click').on('click', this.show.bind(this));
But this has limited browser support . 但这限制了浏览器的支持 。
You can also use a closure: 你也可以使用一个闭包:
var myObj = this;
var show = function () {
myObj.show()
}
$('.click').on('click', show);
When binding a function to an event with jquery, the context in which this function is called is the dom object that has been clicked. 使用jquery将函数绑定到事件时,调用此函数的上下文是已单击的dom对象。
var Obj = {
init: function () {
this.over = $('<div />').addClass('over');
$('body').append(this.over);
var that = this;
$('.click').on('click', function(){
// console.log( this ) will log the dom object
that.show.call( that )
} );
},
show: function () {
console.log(this.over);
}
}
Obj.init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.