+ 8
[Can'tBeSolved] Problem with .method() declaration using Es6 arrow syntax
I'm getting unexpected output when declaring a custom string method using ES6 Syntax. It is working expected when I use older method i.e. using function keyword, but not with arrow function. Cee my code to see problem. https://code.sololearn.com/W98vFyLcBVRv/?ref=app#js
12 Answers
+ 5
Mirielle[ INACTIVE ] Actually, `this` keyword can be used in arrow functions.
Arrow functions do not create a new context for `this`. It will use the outer context. So if an arrow function is enclosed in a regular function, it's context is bound to the outer function's scope.
The following is appropriate:
https://code.sololearn.com/WuO2s2D1yvJX/?ref=app
+ 3
https://code.sololearn.com/csXo9FfFBfo9/?ref=app
+ 3
Good. You have spread enough misinformation already.
1. You cannot use the "this" keyword inside an arrow function
2. you cannot get the current object inside an arrow function with this
These are both wrong according to this code.
https://code.sololearn.com/WuO2s2D1yvJX
3. the prototype of an object from the OP's code referrs to the current object
The prototype is a property of the object that references another object on the heap. It does not refer to the current object.
a = {}
a !== a.prototype
4. the this keyword represented the object that called the function
I don't know what you mean by ‘object that called the function’ but look at this code
https://code.sololearn.com/WC9AdODGWMkC
+ 2
Mirielle[ INACTIVE ] so Saad😔
I was wondering since a long time why I am not being able to do this, finally got the reason.
Thanks for the answer.
+ 2
Mirielle[ INACTIVE ] A better way to express what you are saying is “arrow functions do not have their own context”.
As I have proved earlier, `this` can be used in an arrow function.
Also,
this != current object
this = current context
+ 2
Well, I might have confused you by saying
this = current context
I should have said
this = a reference to the object that owns the current execution context
That is just a longer way of saying the same thing though.
If `this` is the current object, it is expected that `this` remains the same even when a function is bound to another object's context. That does not happen so `this` is not a reference to the current object.
The MDN link you shared explains it clearly.
"""
In most cases, the value of this is determined by how a function is called (runtime binding)... it may be different each time the function is called. ES5 introduced the bind() method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).
"""
Is that not clear enough for you?
+ 1
Mirielle[ INACTIVE ]
I suggest read more about `this` from MDN. W3Schools is not a good learning site. David Carroll probably has more to say on that.
MDN defines `this` as
“A property of an execution context (global, function or eval) that, in non–strict mode, is always a reference to an object and in strict mode can be any value.”
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
See also:
https://www.freecodecamp.org/news/how-to-understand-the-keyword-this-and-context-in-javascript-cd624c6b74b8/
+ 1
Mirielle[ INACTIVE ]
`this` keyword is different for JavaScript. Partly because it existed way before JavaScript supported OOP.
In most languages, `this` refers to the object SCOPE. In JavaScript, `this` refers to the bound CONTEXT.
Context is the lexical environment a function is bound to. Scope is the current context of execution.
https://code.sololearn.com/WC9AdODGWMkC/?ref=app
+ 1
Ah cv NLLDJKFTKIglckv vl Torrescjkcklje Don't spam
0
String.bind(this);
0
MDN defines `this` as
“A property of an execution context (global, function or eval) that, in non–strict mode, is always a reference to an object and in strict mode can be any value.”
I think that is consistent with what I have said all along.
In the section titled ‘Arrow Functions’, the following can be found:
"""
In arrow functions, this retains the value of the enclosing lexical context's this
"""
- 2
Salom