How functions create new functions in Javascript? [duplicate] - javascript

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
In the Eloquent Javascript book I came across this code.
I understood how this works and the passing of arguments but what I am unable to understand is author's statement regarding this code that it's a function which can create another function!
My question is: How is it creating a new function? What is happening which the author is calling creation of a new function? I mean sure we are creating a function called greaterThan and it has another function in it but I can't see how greaterThan is creating another function!
I assure you I have read many similar Qs before asking but couldn't find the answer I am looking for. Thank you for your time & help.
function greaterThan(n) {
return function(m) {
return m > n;
};
}
var greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true

The function is being created on the sixth line.
var greaterThan10 = greaterThan(10);
This creates a function greaterThan10 which can be used to check if numbers are greater than 10. You can see it used on line 7.
Edit:
When the function greaterThan is called on line 6, it returns the nested function, effectively making
greaterThan10 = function(m){
return m > 10;
};
The author was calling greaterThan10 a 'new function' created by the function greaterThan.

Related

Extra parameters in a self executing function? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 1 year ago.
I saw this piece of code in a webpack compiled file
(1,2, function(x) {
console.log(x);
return 4;
})(5);
This seems to execute correctly. I am aware the 5 is a parameter to function(x)
I don't understand what the 1,2 are? How is this valid?
Well, it's because the brackets only returns the function
(thing1,thing2,thing3,thing4....,thingN) would show the last thing on the list(in this case, thingN)
Example
var bracketTest1=(1,3,5,7,9,null)
console.log(bracketTest1) //null
var bracketTest2=(null,1,2,3,4)
console.log(bracketTest2) //4
var bracketTest3=(null,1,2,4,5,function(x){return x})
console.log(bracketTest3) //function(x)
console.log(bracketTest3(Object)) //function Object()
console.log((1,2,3,4,null,undefined)) //prints undefined

Difference between declared function with name and without name [duplicate]

This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Closed 7 years ago.
can anybody tell me whats the difference between this two examples:
//difference with this:
var x = function withName(a,b,c) {} //why i should declare a name here?
//and this
var y = function (a,b,c) {}
According to the ECMA specification, a function can be written either through declaration or through expression,
Declaration is writing function as below,
function withName(a,b,c) {
}
With declaration, it is required to write an identifier which in this case is withName
Both the example that you have given are of function expression where it is not required to write the identifier i.e. withName as you can still call this function with the variable x or y
var x = function withName(a,b,c) {}
var y = function (a,b,c) {}
However, the only difference here is that if you don't specify the identifier you are creating a anonymous funtion.
You can see this link for detailed explanation.
On this particular case, there is no difference. But in general, the fact that the function assigned to a variable has a name, makes it possible to invoke the function from inside the same function (recursion).

Does the way a JS function is defined affect it's "performance"? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.

JavaScript why option 1 doesn't work and option 2 does [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
I ran across these two examples. Example 1 is not valid JavaScript - it throws an error. The second example is valid and works fine. Is there any chance someone can explain why 1 throws an error?
Example 1:
var
laugh
;
laugh();
laugh = function () {
console.log( "Hahaha!!!" );
};
Example 2:
laugh();
function laugh() {
console.log( "Hahaha!!!" );
}
It's something called "hoisting" is javascript. Some blog post on the topic: http://jamesallardice.com/explaining-function-and-variable-hoisting-in-javascript/
Assuming you mean that option 1 is invalid and option 2 is valid:
this is a classic pitfall in JS. basically, in your first function, you declare the variable laugh as an anonymous function, but that doesn't turn it into a function which you can execute by laugh(). however, in the second example, you explicitly declare the function laugh, which you CAN execute by laugh().
edit for below comment: as a correction: you CAN execute the function, but you need to declare it BEFORE executing. else it is undefined. JS makes a list of all functions during compile so they can be executed before they are declared,, but variables are declared and assigned at runtime, so they need to be assigned BEFORE executing.
edit2: vkurchatkin has a good link to what i'm talking about.

There is any different between new F and new F()? [duplicate]

This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 5 years ago.
Think about the silution
function F(){}; //This is a Constructor function
Who can tell me there is any different between
var myInstance = new F;
and
var myInstance = new F();
? The new keyword execute followed Function immediately anyway whatever that is following by partheses ?
There is no difference. From the Mozilla Docs:
new constructor[([arguments])]
The parentheses are in square brackets, that means they are optional.
There is no practical difference, omitting the paranthesis can be done if no arguments are passed to simplify the grammar.
Note that some validators such as JSLint will report a warning if you leave them out though, as it is considered more consistent to always use the same syntax for invoking constructor functions regardless of arguments.
This similar example would be very bad if you get into this lazy habit:
var one = myFunc;
var two = myFunc();
These are two different variables, one is a function reference and the other is the return value of the function.

Categories

Resources