• 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);
               });
            }
        }
    }
    
  • 相关阅读:
    46)父类和子类出现重名变量
    45)子类的构造和析构
    44)子类与父类之间的兼容性问题
    43)继承的方式
    42)子类继承 有参父类 和无参父类
    41)子类和父类的内存关系
    40)类与类之间的关系(has use is)
    jquery学习之1.15-克隆节点
    jquery学习之1.14-删除节点
    jquery学习之1.13-节点插入-外部插入和内部插入
  • 原文地址:https://www.cnblogs.com/chucklu/p/14246583.html
Copyright © 2020-2023  润新知