Why does JavaScript ES6 function calls would not work as expected
The following snipped is given:
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:
"function () {return "
arrow-function
";} "
Can you explain me that?
javascript ecmascript-6
add a comment |
The following snipped is given:
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:
"function () {return "
arrow-function
";} "
Can you explain me that?
javascript ecmascript-6
5
You are writing the method, and not the result of the method, like you do withoutput()
. To make it work, you'll need to invoke the arrow functiondocument.write((() => {return "<p>arrow-function</p>"})())
would work.
– Ori Drori
Nov 13 '18 at 22:14
2
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46
add a comment |
The following snipped is given:
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:
"function () {return "
arrow-function
";} "
Can you explain me that?
javascript ecmascript-6
The following snipped is given:
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:
"function () {return "
arrow-function
";} "
Can you explain me that?
javascript ecmascript-6
javascript ecmascript-6
asked Nov 13 '18 at 22:12
Marc MMarc M
5281623
5281623
5
You are writing the method, and not the result of the method, like you do withoutput()
. To make it work, you'll need to invoke the arrow functiondocument.write((() => {return "<p>arrow-function</p>"})())
would work.
– Ori Drori
Nov 13 '18 at 22:14
2
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46
add a comment |
5
You are writing the method, and not the result of the method, like you do withoutput()
. To make it work, you'll need to invoke the arrow functiondocument.write((() => {return "<p>arrow-function</p>"})())
would work.
– Ori Drori
Nov 13 '18 at 22:14
2
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46
5
5
You are writing the method, and not the result of the method, like you do with
output()
. To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})())
would work.– Ori Drori
Nov 13 '18 at 22:14
You are writing the method, and not the result of the method, like you do with
output()
. To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})())
would work.– Ori Drori
Nov 13 '18 at 22:14
2
2
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46
add a comment |
4 Answers
4
active
oldest
votes
You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
add a comment |
It's because you are creating the function but not calling it.
To call the anonymous arrow function you should do:
(() => {return "<p>arrow-function</p>"})()
add a comment |
You are telling document.write
to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting
var f = () => {return "<p>arrow-function</p>"};
document.write(f())
add a comment |
It is because the first parameter is a function call, whereas the second is only a function.
For the second call to work as the first, you would do:
document.write( (() => {return "<p>arrow-function</p>"})() );
This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.
This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function
statements but you can immediately call arrow functions too.
On the other hand, for the first line of your snippet to work as your original second, you would do instead:
document.write(output);
So now you're passing just two function definitions to document.write
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290287%2fwhy-does-javascript-es6-function-calls-would-not-work-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
add a comment |
You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
add a comment |
You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
function output() {
return "<p>normal function</p>";
}
//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});
//should be
document.write((() => {return "<p>arrow-function</p>"})());
answered Nov 13 '18 at 22:16
Jordan SJordan S
3,069921
3,069921
add a comment |
add a comment |
It's because you are creating the function but not calling it.
To call the anonymous arrow function you should do:
(() => {return "<p>arrow-function</p>"})()
add a comment |
It's because you are creating the function but not calling it.
To call the anonymous arrow function you should do:
(() => {return "<p>arrow-function</p>"})()
add a comment |
It's because you are creating the function but not calling it.
To call the anonymous arrow function you should do:
(() => {return "<p>arrow-function</p>"})()
It's because you are creating the function but not calling it.
To call the anonymous arrow function you should do:
(() => {return "<p>arrow-function</p>"})()
answered Nov 13 '18 at 22:15
Ariel AlvaradoAriel Alvarado
682311
682311
add a comment |
add a comment |
You are telling document.write
to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting
var f = () => {return "<p>arrow-function</p>"};
document.write(f())
add a comment |
You are telling document.write
to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting
var f = () => {return "<p>arrow-function</p>"};
document.write(f())
add a comment |
You are telling document.write
to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting
var f = () => {return "<p>arrow-function</p>"};
document.write(f())
You are telling document.write
to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting
var f = () => {return "<p>arrow-function</p>"};
document.write(f())
answered Nov 13 '18 at 22:16
Matti PriceMatti Price
2,012919
2,012919
add a comment |
add a comment |
It is because the first parameter is a function call, whereas the second is only a function.
For the second call to work as the first, you would do:
document.write( (() => {return "<p>arrow-function</p>"})() );
This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.
This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function
statements but you can immediately call arrow functions too.
On the other hand, for the first line of your snippet to work as your original second, you would do instead:
document.write(output);
So now you're passing just two function definitions to document.write
.
add a comment |
It is because the first parameter is a function call, whereas the second is only a function.
For the second call to work as the first, you would do:
document.write( (() => {return "<p>arrow-function</p>"})() );
This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.
This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function
statements but you can immediately call arrow functions too.
On the other hand, for the first line of your snippet to work as your original second, you would do instead:
document.write(output);
So now you're passing just two function definitions to document.write
.
add a comment |
It is because the first parameter is a function call, whereas the second is only a function.
For the second call to work as the first, you would do:
document.write( (() => {return "<p>arrow-function</p>"})() );
This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.
This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function
statements but you can immediately call arrow functions too.
On the other hand, for the first line of your snippet to work as your original second, you would do instead:
document.write(output);
So now you're passing just two function definitions to document.write
.
It is because the first parameter is a function call, whereas the second is only a function.
For the second call to work as the first, you would do:
document.write( (() => {return "<p>arrow-function</p>"})() );
This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.
This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function
statements but you can immediately call arrow functions too.
On the other hand, for the first line of your snippet to work as your original second, you would do instead:
document.write(output);
So now you're passing just two function definitions to document.write
.
edited Nov 15 '18 at 15:50
answered Nov 13 '18 at 22:16
SergeonSergeon
2,515818
2,515818
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290287%2fwhy-does-javascript-es6-function-calls-would-not-work-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
You are writing the method, and not the result of the method, like you do with
output()
. To make it work, you'll need to invoke the arrow functiondocument.write((() => {return "<p>arrow-function</p>"})())
would work.– Ori Drori
Nov 13 '18 at 22:14
2
Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich
– Marc M
Nov 13 '18 at 22:36
There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.
– Marc M
Nov 13 '18 at 22:46