Git Product home page Git Product logo

Comments (8)

fhalde avatar fhalde commented on August 15, 2024
function test() {
  this.nums = [1, 2, 3];
  this.fives = [];
  this.nums.forEach((v) => {
       if (v % 5 === 0)
           this.fives.push(v)
   })
}

new test();

from es6-features.

rse avatar rse commented on August 15, 2024

Why do you think the examples assume a global context? "this" references the context, independent whether it is the global context or a local context of a function. If the code snippets are run in global contect then "this" is actually "global" (Node.js) or "window" (Browser), yes. But if the same code snippets are run in a local context then this is actually the context of the surrounding function. For the illustration of lexical "this", does it matter in your opinion? The point is not that "this" is a context pointer, but that one does not need helper variables like "self" to be able to use it inside a callback function.

from es6-features.

fhalde avatar fhalde commented on August 15, 2024

ok the es5 variant 1 would've worked without introducing the self variable too! That confused me.

And I thought well

Removing the self variable and just using the this straightaway is fine too. What is the point this example is trying to convey?

from es6-features.

rse avatar rse commented on August 15, 2024

No, in the following variante 1 it would not work without "self":

5| //  variant 1
5| var |self| = |this|;
5| |this|.nums.forEach(|function (v)| {
5|     if (v % 5 === 0)
5|         |self|.fives.push(v);
5| });

Because without "self" an inner "this.fives.push(v)" would reference the context of the "function (v)" and not the one which is referenced by "this.nums...".

from es6-features.

fhalde avatar fhalde commented on August 15, 2024

Boy this was a bummer. Thanks! @rse

Now I dont understand the following

/* global context */

this.fives = [];
function(v) {
 this.fives.push(v);
}

this refers to global

but

/* global context */
this.fives = []
[1,2,3].forEach(function(v) {
  this.fives.push(v);
});

this refers to ???

from es6-features.

rse avatar rse commented on August 15, 2024

No, even in your first code snippet the "this.fives" does NOT reference the "this.fives" outside the function. The point is that "this" inside a "function" (in contrast to an ES6 arrow function!!) points to a local context of the function. So, in your first code snippet "this" ouside the function references the global scope, but "this" inside the function references the local scope of the function.

In your second code snippet this is not different. It makes no difference for the function whether it is a plain function (as in the first snippet) or a callback. It will always have its own local context, except the function is called as a method on an object. But .foreach() does not do this. It just calls the function and hence the function still has its local scope.

from es6-features.

fhalde avatar fhalde commented on August 15, 2024

hmmm i remember doing the following exercise in node

file a.js

var self = this;
console.log(this);
function hello() {
  console.log(this);
  console.log(this == self); // false !!!!!
}

both printed the same global object but the equality was wrong

from es6-features.

rse avatar rse commented on August 15, 2024

Yes, of course! Once again: "this" inside the function is a different scope than "this" outside the function. So, obviously "this === self" is "false", of course. That's why one used the "self" variable in ES5. And that's the point of ES6 arrow functions: Their "this" is the same as the "this" outside of it, so no more "self" is needed.

from es6-features.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.