Why this is working:
var a = () => {
var print = function(i) { console.log(i); return this; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
a().print(5).print2(5);
this is also working:
var b = () => {
var print = (i) => { console.log(i); return this; }
return { print:print}
}
b().print('Arrow function works');
while this is not working:
var b = () => {
var print = (i) => { console.log(i); return this; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
b().print(5).print2(5);
/
Why this is working:
var a = () => {
var print = function(i) { console.log(i); return this; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
a().print(5).print2(5);
this is also working:
var b = () => {
var print = (i) => { console.log(i); return this; }
return { print:print}
}
b().print('Arrow function works');
while this is not working:
var b = () => {
var print = (i) => { console.log(i); return this; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
b().print(5).print2(5);
https://jsfiddle/Imabot/1gt2kxfh/14/
Share Improve this question edited Jan 25, 2020 at 17:00 Fifi asked Jan 25, 2020 at 16:52 FifiFifi 3,6153 gold badges31 silver badges62 bronze badges 3- 1 Because that's how arrow functions work (and indeed is the main reason for why they were introduced - contrary to popular belief, it's nothing to do with saving keystrokes). See developer.mozilla/en-US/docs/Web/JavaScript/Reference/…, amongst others – Robin Zigmond Commented Jan 25, 2020 at 16:56
- I updated the question with a new working example. – Fifi Commented Jan 25, 2020 at 17:01
-
In the print function constructed with an arrow syntax the
this
references the scope ofb
. While in the function declarationthis
refers to it's own scope. – Mouser Commented Jan 25, 2020 at 17:03
3 Answers
Reset to default 5It's all due to arrow functions behavior(docs)
Step by step explanation:
var b = () => {
// 1
var print = (i) => { console.log(i); return this; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
const res = b()
// 2
const secondRes = res.print(5)
// 3
secondRes.print2(5);
- here
print
function savesthis
reference from the outer scope, sothis
can't be reassigned anymore - now
print
function is not usingthis
reference that es fromres
variable, becausethis
has already been attached toprint
function above - as a result
secondRes
is not going to reference to the object that was returned byb
function. But it will usethis
reference that is attached toprint
function. And finally becausesecondRes
doesn't haveprint2
property - it throws
In a non-arrow function, the value of this
depends on how the function is called. If the function is called as a member of an object, this
refers to this object:
someObj.myFunction() // inside myFunction this will point to someObj
In contrast, the arrow functions do not affect this
. So whithin an arrow function the value of this
is whatever it is in the enclosing scope.
The answer from Lex82 gives the why. If you want to return the functions, you can use function chaining:
var b = () => {
var print = (i) => { console.log(i); return { print:print , print2:print2 }; }
var print2 = function(i) { console.log(i); return this; }
return { print:print , print2:print2 }
}
b().print(5).print2(5);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420452a4440577.html
评论列表(0条)