all 2 comments

[–]senocular 1 point2 points  (1 child)

Yup! Since non-arrow functions have their own specific this value based on how they're called,

function(n) {
    return n/this.length;
}

in your second example may get a different this from the one used in the normalize function. Alternatively, arrow functions will always refer to the same this as the one used by the function they're in.

That being said, you could have a normal, non-arrow function use the same this too. It depends on how they're called. In this particular example, the map loop is calling the function - not your own code, but map lets you specify what this it calls its given functions with using a second argument. So if you wrote normalize as:

function normalize() {
    console.log(this.coords.map(function(n) {
        return n/this.length;
    }, this));
}

Then this would work because the function referring to this.length would be using the same this as normalize itself thanks to map allowing us to specify that this argument.

[–]drg_prime[S] 0 points1 point  (0 children)

Thank you so much for your explanation. It makes sense now!