This question already has answers here:
What is return new function(); in JavaScript? [duplicate]
(1 answer)
Is it right to think of a Javascript Function Expression that uses the 'new' keyword as 'static'
(3 answers)
Closed 3 years ago.
I've seen this a few times - in a top level function an old programmer at my job used to do the following:
function x() {
return new function() {
//some code
}
}
What is the difference between that and:
function x() {
return function() {
//some code
}
}
Wouldn't returning an anonymous function without new instantiate a new function every time? It's not currying, so I'm not sure I see a difference.
Thanks
The first function return a value of type "object" and the second return a value of type "function".
I think this piece of code can help you
console.log(typeof(function(){})) // "function"
console.log(typeof(new function(){})) // "object"
Two type of differents value so the behavior is totally different.
Related
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]
(4 answers)
Closed 4 years ago.
Can anybody explain below code? All I know is it is anonymous function but what is (0) doing?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
Why output of above code comes zero. Can anybody explain?
That's because what you're doing is creating a function and then calling it immediately where x = 0. Your function is returning x, and thus 0.
As for what anonymous functions are, they are basically a function that gets stored in a variable. You call it from the variable instead of by name. So, for example:
var output = function (x) { return x;};
Can be called like this:
output(0);
As opposed to the normal way like this:
function myOutput(x) {
return x;
}
myOutput(0);
This question already has answers here:
Why JavaScript functions always return a value?
(4 answers)
Closed 5 years ago.
Out of curiosity, why does writing console.log("hello") inside console return undefined?
Is it for the same reasons in defining void function in C?
The console.log just write a text and return with undefined.
If you create a function, add a returning value, there will be not undefined, it will returns with the added value.
Example:
function writer(){
console.log("write new line");
return "ok";
}
If you call the writer() that the output is "ok" in new line after "write new line".
This question already has answers here:
declare function name in object, Why? [duplicate]
(3 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
First of all, sorry for bad title or description, I'm not completely good with English.
I want to know what is difference between this line of code:
var obj = {
...
func: function func() { ... },
...
}
and this:
var obj = {
...
func: function() { ... },
...
}
What is it special in naming a method twice? I saw both of these ways in a single JavaScript source code. Here you can take a look at source if it's needed.
Edit: Question is not about anonymous or non-anonymous function declaring, but about functions inside objects that are called methods.
One of the biggest (and most helpful) differences is that the non-anonymous function will provide the function name in stack traces.
The named version can be used recursively as Teemu points out.
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
Hello I've got a function like this one.
function doSomething () {
( function () {
// callbacks and code
}
)();
}
I am not use to that typing, I'm looking forward to understand how is interpreted by the browser or what does it mean the ( function () ) ();
Thought I know the differences between calls and assigns , I can't read this properly , surely is something simple but I don't really get it.
Thanks.
Is a self invoked anonymous function:
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous. Here is the format of self-executing anonymous function:
(function(){
// some codeā¦
})();
https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.