Sign in
Log inSign up

The case of disappearing this pointer

Reza Razavipour's photo
Reza Razavipour
·Feb 7, 2017

I have a class with a member function and a member variable. In the member function, I display the this pointer and it is valid. In the same routine, inside a foreach loop, the this pointer becomes undefined

The code is

public getTypes(vitalsModel) : Array<any>{
    var vitals = _.get(vitalsModel, 'data');
    if (_.isEmpty(vitals) || !_.isArray(vitals)) {
        return [];
    }

   // the this pointer is valid
    _.forEach(vitals, function (vital) {
            let setting = _.get(vitalsModel, key);

          // here the this pointer is undefined.
            this.generalSettings.push(setting);

        console.log(keys);
        rows.push(keys);
        loc++;
    });

Anyone sees what i am doing wrong?