I'm just getting started in OO javascript so please bear with me.
我刚刚开始使用OO javascript,所以请耐心等待。
This works:
如此:
var myObj = {
foo : function() {
alert('hello');
this.bar();
},
bar: function() {
alert('world');
}
}
However if I do some other stuff after the hello alert in the "foo" method then the meaning of "this" changes from the object to whatever I last selected so using this.bar()
doesn't execute the other method in the class.
但是,如果我在“foo”方法的hello alert之后做一些其他的事情,那么“this”的含义就会从对象变为我最后选择的对象,所以使用this.bar()不会执行类中的其他方法。
So I tried to cache "this" in a variable like so:
所以我试着将"this"缓存到如下的变量中:
var myObj = {
publicVars: {
theObj : this
},
foo : function() {
alert('hello');
publicVars.theObj.bar();
},
bar: function() {
alert('world');
}
}
But that didn't work either. So what is the solution?
但这也不管用。那么解决方案是什么呢?
EDIT
Here is my actual code:
这是我的实际代码:
var formObj = {
validate : function(theForm) {
$('input, textarea', theForm).each(function() {
var valueLength = $(this).val().length;
if (valueLength === 0) {
$(this).addClass('invalid');
this.listenForInput($(this)); // <!------- this isn't working
}
});
},
listenForInput : function(theField) {
// theField.keyup(function() {
// if ($(this).val().length > 0) {
// theField.removeClass('invalid');
// }
// });
alert('I work!!!!');
}
} // end obj
3 个解决方案
#1
5
As I said in my comment, you have to keep a reference inside the function:
正如我在评论中所说,你必须在函数中保留一个引用:
validate: function(theForm) {
var self = this;
$('input, textarea', theForm).each(function() {
var valueLength = $(this).val().length;
if (valueLength === 0) {
$(this).addClass('invalid');
self.listenForInput($(this));
}
});
},
You are passing a function to each
. Inside this callback, this
refers to the DOM element. That's why you pass it to jQuery ($(this)
) to be able to call jQuery methods on that element. It cannot refer to formObj
too!
你给每个人传递一个函数。在这个回调中,它引用DOM元素。这就是为什么您将它传递给jQuery ($(this)),以便能够调用该元素上的jQuery方法。它也不能指formObj !
What this
refers to is determined by how a function is called and each function has its own this
(the Mozilla documention describes this
in more detail).
这是指一个函数被调用的方式,每个函数都有它自己的功能(Mozilla文档更详细地描述了这一点)。
If you call validate
with formObj.validate()
, then this
refers to formObj
.
如果您使用formObj.validate()调用validate,则它引用formObj。
The jQuery documentation for each
states:
每个状态的jQuery文档:
More importantly, the callback is fired in the context of the current DOM element, so the keyword
this
refers to the element.更重要的是,回调在当前DOM元素的上下文中触发,因此关键字this指代元素。
#2
2
Am I missing something, or can you not just reference the object by name, like this:
我是不是漏掉了什么,或者你不能直接按名字引用这个对象,像这样:
var myObj = {
foo: function() {
alert('hello');
myObj.bar();
},
bar: function() {
alert('world');
}
}
myObj.foo();
http://jsfiddle.net/karim79/kaXYj/
http://jsfiddle.net/karim79/kaXYj/
#3
0
A function's this keyword is set by the call, it can't change during function execution.
函数的这个关键字是由调用设置的,在执行函数时不能改变。
Calling obj.foo()
sets foo's this to obj so calling this.bar calls obj.bar. However, if you call foo some other way, e.g.:
调用object。foo()将foo's设为obj所以调用这个。obj.bar栏调用。然而,如果你用别的方式称呼foo,例如:
var a = obj.foo;
a();
then its this will may be obj (in the above case it will be window or undefined in strict mode) so you get a different bar or an error if the this object doesn't have a bar property.
然后它可能是obj(在上面的例子中它将是窗口或者在严格模式下没有定义)所以如果这个对象没有bar属性你会得到一个不同的bar或者一个错误。
#1
5
As I said in my comment, you have to keep a reference inside the function:
正如我在评论中所说,你必须在函数中保留一个引用:
validate: function(theForm) {
var self = this;
$('input, textarea', theForm).each(function() {
var valueLength = $(this).val().length;
if (valueLength === 0) {
$(this).addClass('invalid');
self.listenForInput($(this));
}
});
},
You are passing a function to each
. Inside this callback, this
refers to the DOM element. That's why you pass it to jQuery ($(this)
) to be able to call jQuery methods on that element. It cannot refer to formObj
too!
你给每个人传递一个函数。在这个回调中,它引用DOM元素。这就是为什么您将它传递给jQuery ($(this)),以便能够调用该元素上的jQuery方法。它也不能指formObj !
What this
refers to is determined by how a function is called and each function has its own this
(the Mozilla documention describes this
in more detail).
这是指一个函数被调用的方式,每个函数都有它自己的功能(Mozilla文档更详细地描述了这一点)。
If you call validate
with formObj.validate()
, then this
refers to formObj
.
如果您使用formObj.validate()调用validate,则它引用formObj。
The jQuery documentation for each
states:
每个状态的jQuery文档:
More importantly, the callback is fired in the context of the current DOM element, so the keyword
this
refers to the element.更重要的是,回调在当前DOM元素的上下文中触发,因此关键字this指代元素。
#2
2
Am I missing something, or can you not just reference the object by name, like this:
我是不是漏掉了什么,或者你不能直接按名字引用这个对象,像这样:
var myObj = {
foo: function() {
alert('hello');
myObj.bar();
},
bar: function() {
alert('world');
}
}
myObj.foo();
http://jsfiddle.net/karim79/kaXYj/
http://jsfiddle.net/karim79/kaXYj/
#3
0
A function's this keyword is set by the call, it can't change during function execution.
函数的这个关键字是由调用设置的,在执行函数时不能改变。
Calling obj.foo()
sets foo's this to obj so calling this.bar calls obj.bar. However, if you call foo some other way, e.g.:
调用object。foo()将foo's设为obj所以调用这个。obj.bar栏调用。然而,如果你用别的方式称呼foo,例如:
var a = obj.foo;
a();
then its this will may be obj (in the above case it will be window or undefined in strict mode) so you get a different bar or an error if the this object doesn't have a bar property.
然后它可能是obj(在上面的例子中它将是窗口或者在严格模式下没有定义)所以如果这个对象没有bar属性你会得到一个不同的bar或者一个错误。