How to function call using 'this' inside forEach loop
How to function call using 'this' inside forEach loop
In the following object, I have a problem using the 'this' reference:
function SampleObject(){
this.addObject = function(object){...}
...
// more code here
...
this.addNewObjects= function(arr){
arr.forEach( function (obj) {
this.addObject(new Obj(obj.prop1, obj.prop2));
});
}
}
I'm assuming the context is changing and that 'this' refers the iterated 'obj', and not 'SampleObject'. I've solved the problem using a normal for loop however, i'm curuois to why this is not working, and would like to know if there is another way to do this.
回答
You can store this in variable:
var self = this;
this.addNewObjects = function(arr){
arr.forEach(function(obj) {
self.addObject(new Obj(obj.prop1, obj.prop2));
});
}
or use bind:
this.addNewObjects = function(arr) {
arr.forEach(function(obj) {
this.addObject(new Obj(obj.prop1, obj.prop2));
}.bind(this));
}
And side note, without those this
will be window object not obj. This
is always object that was created using new keyword or window object if it's normal function. In strict mode this
will be undefined in this case.
UPDATE: and with ES6 you can use arrow function:
this.addNewObjects = function(arr) {
arr.forEach((obj) => {
this.addObject(new Obj(obj.prop1, obj.prop2));
});
}
arrow functions don't have their own this
and they get it from outer scope.
UPDATE2: from @viery365 comment you can use this as second argument to forEach and it will make context for the function:
this.addNewObjects = function(arr) {
arr.forEach(function(obj) {
this.addObject(new Obj(obj.prop1, obj.prop2));
}, this);
}
You can read this on MDN forEach page
Jquery $().each method obscures 'this' keyword
I am creating a Javascript object that contains a function that executes a jQuery each
method like the following:
function MyClass {
Method1 = function(obj) {
// Does something here
}
Method2 = function() {
$(".SomeClass").each(function() {
// 1 2
this.Method1(this);
});
}
}
Which object is each THIS
referring to? jQuery is referring to the item returned from the each
iteration. However, I would like This[1]
to refer to the containing class...
How can I refer to the containing class from within the jQuery loop?
回答:
I guess you could do something like this:
function MyClass {
Method1 = function(obj) {
//do something here
}
Method2 = function () {
var containingClass = this;
$(".SomeClass").each(function () {
containingClass.Method1(this);
});
}
}
}
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-01-07 ssms18清除记住的账号和密码
2019-01-07 uniqueidentifier in SQL becomes lower case in c#
2016-01-07 VS2015中添加新建项,找不到ado .net entity datamodel的解决方法
2016-01-07 vs2015中安装EntityFramework