admin管理员组文章数量:1313991
I am looking for var self = this alternative plan.
var Animal = function(name){
this.name = name;
this.arr = [1,2,3,4];
this.inc = function(num){
return num + 1;
};
this.fireArr = function(){
var self = this;
this.arr.forEach(function(item){
console.log(self.inc(item));
});
};
};
var dog = new Animal("dog");
console.log(dog.fireArr());
My fiddle is here.
/
Do you have any idea?
Thanks in advance.
I am looking for var self = this alternative plan.
var Animal = function(name){
this.name = name;
this.arr = [1,2,3,4];
this.inc = function(num){
return num + 1;
};
this.fireArr = function(){
var self = this;
this.arr.forEach(function(item){
console.log(self.inc(item));
});
};
};
var dog = new Animal("dog");
console.log(dog.fireArr());
My fiddle is here.
http://jsfiddle/haradashinya/TtYpc/
Do you have any idea?
Thanks in advance.
Share Improve this question edited Aug 5, 2012 at 17:43 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Aug 5, 2012 at 17:38 nobinobirunobinobiru 7921 gold badge12 silver badges29 bronze badges3 Answers
Reset to default 6You can set the second argument to forEach
, which is the this
value.
this.arr.forEach(function(item){
console.log(this.inc(item));
}, this);
You can use .bind()
to make sure the function is called with the right this
value:
function fireArr() {
this.arr.forEach(function(item){
console.log(this.inc(item));
}.bind(this));
}
But imho the self
(that
, _this
) variable is easier to understand, because it directly states that not the normal this
value is used, although one would expect it (e.g. in an event handler, or jQuery's each()
). Especially on long functions, where you don't see the bind()
in the end, this is of importance. Also, some ancient browsers do not support bind()
and you would need to shim it.
So, for any in-place function expressions I remend the use of a dereferencing variable.
But it can be of great use when you have a method defined somewhere, normally using this
to point to the current object as it is mon in that context, and then the method should be used somewhere else. Instead of a var self
-wrapper, you can and should use bind
for simplicity and clarity. Your example offers quite a good demo (assuming the inc
method used the this
keyword):
this.arr.forEach( this.inc.bind(this) );
(although forEach()
allows us to pass a custom this
argument - event attachers for example don't)
In your example, the inc
function doesn't use the this
value, so it doesn't need to be a method. You can define it as a local function:
var Animal = function ( name ) {
this.name = name;
this.arr = [ 1, 2, 3, 4 ];
var inc = function ( num ) {
return num + 1;
};
this.fireArr = function () {
this.arr.forEach(function ( item ) {
console.log( inc( item ) );
});
};
};
本文标签: javascriptalternative for the var selfthis pattern Stack Overflow
版权声明:本文标题:javascript - alternative for the var self = this pattern- Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741922686a2405100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论