Accessing this in a forEach loop results in undefined

后端 未结 3 938
不思量自难忘°
不思量自难忘° 2020-11-30 06:21

I\'m iterating through an array using forEach in one of my Class\'s methods. I need access to the instance of the class inside the forEach but this is undef

相关标签:
3条回答
  • 2020-11-30 06:24

    What I had to to is add this in the every forEach that I was using (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach ). Binding in the constructor is not needed, as I am using arrow functions. So now my code is:

    resetPressed = () => {
        this.transport_options.forEach(function (transport_option) {
            this.pressed_percentages.forEach(function (percentage) {
                filters[transport_option][percentage] = false;
            }, this)
        }, this);
    
        filters.isFilterActive = false;
        this.setState({
            filtersState: filters,
            opacity: filters.isFilterActive ? 1 : 0.5
        });
    
    }
    
    
    <TouchableHighlight
        underlayColor={'transparent'}
        onPress={this.resetPressed}
        style={styles.iconView}>
    
    0 讨论(0)
  • 2020-11-30 06:32

    Since you're using strict mode, when a function is called that isn't a property of an object, this will have the value undefined by default (not the global object). You should store its value manually:

    var aGlobalVar = {};
    
    (function () {
        "use strict";
    
        aGlobalVar.thing = function () {
            this.value = "thing";   
        };
    
        aGlobalVar.thing.prototype.amethod = function () {
            var self = this;
            data.forEach(function (element) {
              console.log(element);
              console.log(self.value);
            });
        };
    })();
    
    var rr = new aGlobalVar.thing();
    rr.amethod();
    

    Nowadays, with ES2015 you can also use arrow functions, which uses the this value of the outside function:

    function foo() {
      let bar = (a, b) => {
        return this;
      };
    
      return bar();
    }
    
    foo.call(Math); // Math
    

    T.J. Crowder's solution of using the second argument of forEach also works nicely if you don't like the idea of the temporary variable (ES5 code: works in pretty much any browser these days, except IE8-).

    0 讨论(0)
  • 2020-11-30 06:44

    In strict mode if you call a function not through a property reference and without specifying what this should be, it's undefined.

    forEach (spec | MDN) allows you to say what this should be, it's the (optional) second argument you pass it:

    aGlobalVar.thing.prototype.amethod = function() {
      data.forEach(function(d) {
        console.log(d);
        console.log(this.value);
      }, this);
      // ^^^^
    }
    

    Alternately, arrow functions were added to JavaScript in 2015. Since arrows close over this, we could use one for this:

    aGlobalVar.thing.prototype.amethod = function() {
      data.forEach(d => {
        console.log(d);
        console.log(this.value);
      });
    }
    
    0 讨论(0)
提交回复
热议问题