Comments (8)
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.
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.
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.
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.
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.
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.
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.
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)
- Could you please add es7 and later feature overview & comparison? HOT 6
- confusing variable names HOT 1
- PDF Version HOT 1
- Object initializer shorthand for sub-properties?
- var instead of let
- Certificate error on https://es6-features.org/
- Rename spread operator to spread syntax
- Evaluating string interpolation for template literals
- Fibonacci numbers issue
- ES6 keywords behaves as variable
- Misleading test of value presence in WeakSet/WeakMap
- Non-working example of Typed Arrays
- Map/Set & WeakMap/WeakSet - Uncaught ReferenceError: s is not defined HOT 2
- I18n HOT 1
- Array.prototype.findIndex
- Destructuring Assignment/Array Matching sample does not work as expected on the 'reduced' version HOT 1
- EC5 Spead Operator Sample Unused Variable HOT 1
- Gitbook Version
- Large Red 'X' implies ES5 snippets contain errors HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from es6-features.