Which part of the code executes inner function? [duplicate]
This question already has an answer here:
What does a comma do in JavaScript expressions?
5 answers
What is the (function() { } )() construct in JavaScript?
23 answers
I came across this question in one of the online quizes. The solution I came up with was function. BUT, the console logs number.
var f = (
function f(){return "1";},
function g (){return 2;}
)();
console.log(typeof f);
My thinking was: var f
becomes an IIFE, so typeof f
should be a function.
Now, apparently, function g()
inside var f
executes and returns its value to var f
, so the console logs number (or 2, if I just log f
).
Questions:
1.What is the explanation for function comma anotherFunction. This is not an object, and also not a variable declaration (like var x, y;)?
2.Somehow the second function is the one that is being returned, but what part of the code is actually executing it?
(I thought that ()
just before the console.log
call is executing the outer f
function (which should just define inner functions f()
and g()
, but not execute any of them ). The second one gets executed, so , obviously, I was wrong.
javascript iife
marked as duplicate by melpomene, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 8 more comments
This question already has an answer here:
What does a comma do in JavaScript expressions?
5 answers
What is the (function() { } )() construct in JavaScript?
23 answers
I came across this question in one of the online quizes. The solution I came up with was function. BUT, the console logs number.
var f = (
function f(){return "1";},
function g (){return 2;}
)();
console.log(typeof f);
My thinking was: var f
becomes an IIFE, so typeof f
should be a function.
Now, apparently, function g()
inside var f
executes and returns its value to var f
, so the console logs number (or 2, if I just log f
).
Questions:
1.What is the explanation for function comma anotherFunction. This is not an object, and also not a variable declaration (like var x, y;)?
2.Somehow the second function is the one that is being returned, but what part of the code is actually executing it?
(I thought that ()
just before the console.log
call is executing the outer f
function (which should just define inner functions f()
and g()
, but not execute any of them ). The second one gets executed, so , obviously, I was wrong.
javascript iife
marked as duplicate by melpomene, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
you should remove the space after theg
– andrea-f
Nov 21 '18 at 19:15
1
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
1
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
1
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
1
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23
|
show 8 more comments
This question already has an answer here:
What does a comma do in JavaScript expressions?
5 answers
What is the (function() { } )() construct in JavaScript?
23 answers
I came across this question in one of the online quizes. The solution I came up with was function. BUT, the console logs number.
var f = (
function f(){return "1";},
function g (){return 2;}
)();
console.log(typeof f);
My thinking was: var f
becomes an IIFE, so typeof f
should be a function.
Now, apparently, function g()
inside var f
executes and returns its value to var f
, so the console logs number (or 2, if I just log f
).
Questions:
1.What is the explanation for function comma anotherFunction. This is not an object, and also not a variable declaration (like var x, y;)?
2.Somehow the second function is the one that is being returned, but what part of the code is actually executing it?
(I thought that ()
just before the console.log
call is executing the outer f
function (which should just define inner functions f()
and g()
, but not execute any of them ). The second one gets executed, so , obviously, I was wrong.
javascript iife
This question already has an answer here:
What does a comma do in JavaScript expressions?
5 answers
What is the (function() { } )() construct in JavaScript?
23 answers
I came across this question in one of the online quizes. The solution I came up with was function. BUT, the console logs number.
var f = (
function f(){return "1";},
function g (){return 2;}
)();
console.log(typeof f);
My thinking was: var f
becomes an IIFE, so typeof f
should be a function.
Now, apparently, function g()
inside var f
executes and returns its value to var f
, so the console logs number (or 2, if I just log f
).
Questions:
1.What is the explanation for function comma anotherFunction. This is not an object, and also not a variable declaration (like var x, y;)?
2.Somehow the second function is the one that is being returned, but what part of the code is actually executing it?
(I thought that ()
just before the console.log
call is executing the outer f
function (which should just define inner functions f()
and g()
, but not execute any of them ). The second one gets executed, so , obviously, I was wrong.
This question already has an answer here:
What does a comma do in JavaScript expressions?
5 answers
What is the (function() { } )() construct in JavaScript?
23 answers
javascript iife
javascript iife
asked Nov 21 '18 at 19:13
YozexYozex
438
438
marked as duplicate by melpomene, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by melpomene, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
you should remove the space after theg
– andrea-f
Nov 21 '18 at 19:15
1
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
1
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
1
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
1
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23
|
show 8 more comments
you should remove the space after theg
– andrea-f
Nov 21 '18 at 19:15
1
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
1
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
1
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
1
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23
you should remove the space after the
g
– andrea-f
Nov 21 '18 at 19:15
you should remove the space after the
g
– andrea-f
Nov 21 '18 at 19:15
1
1
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
1
1
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
1
1
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
1
1
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23
|
show 8 more comments
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
you should remove the space after the
g
– andrea-f
Nov 21 '18 at 19:15
1
There is no "outer function".
– melpomene
Nov 21 '18 at 19:15
1
Who in their right mind would even write such a thing...
– Ben Steward
Nov 21 '18 at 19:16
1
maybe you have a look to the comma operator.
– Nina Scholz
Nov 21 '18 at 19:19
1
@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"
– Spencer Wieczorek
Nov 21 '18 at 19:23