This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I cannot think of the term that JavaScript developers use for this kind of practice.
var someSome = (function () { ... } ());
-- Wrapping function within brackets. I vaguely remember someone was calling this fiif? fiff?
And there were many advantages and it was recommended java scripting practice.
Anyone got clue what I am talking about and why is it a good practice?
it might have been even without assignment like below
(function () { ... } ());
Self executing function. Often used as a wrapper around a function block to immediately invoke it, as well as give it closure.
Related
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 6 years ago.
When I run a function in js it seems to work fine but will this be fine?
function myfunction(){
}
VS
var myfunction = function(){
}
Because both are called the same so does it make no difference?
Sorry new to js so i'm just making sure it's good to learn this style. (The first function is what i'd prefer to use.)
This question is already asked at this link,
One of the basic difference comes from declaration, for instance :-
foo();
var foo = function(){
console.log('hi');
}
would lead to undefined, since it is called before it's declared, on the other hand,
foo();
function foo(){
console.log('hi');
}
would console 'hi' with no problem, for further insight check the above link.
As shown in the W3Schools Function guide
You can use a function declaration or a function expression.
Both are the same in general. Thought there are certain things to consider:
In a function expression the function is only defined when that line in code is reached
In a function decleration the function is defined as soon as the code is run, so it can be called before it is written in your code (due to hoisting)
The first example you wrote is a function Declaration
The second example you wrote is a function Expression
The only difference between the two is how you will invoke them in your code after you defined them.
If you are wondering which is better, it all comes down to which you are more comfortable with and think the syntax for is more readable, also try not to mix up the two to keep conventions in your code.
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
I often use the closure syntax
var something = (function () {
//TODO: do something
}());
and, I often find people use this syntax
var something = (function () {
//TODO: do something
})();
If both the two behaves the same way, what are the differences between the two?
There's no real difference. Both statements contain function expressions that evaluate to functions that are immediately executed.
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
I looked at the jsf.js file of Mojarra 2.2.8 and saw them using the module pattern. Something like this:
name.space = function() {
var utilFunction = function utilFunction() {
// some implementation
};
return {
exposedFunction: function exposedFunction() {
// using utilFunction
}
};
}();
Is there any benefit of giving the functions a name? As opposed to use anonymous functions. They are bound to either a variable or a property of the same name anyway.
Is this some kind of best practice? Does it maybe improve debugging?
I'm just asking, because I usually see the module pattern used with anonymous functions, and was now wondering.
I think it is justified only when using anonymous functions for obvious reading, for example:
async.waterfall[
function makeOne () {},
function makeTwo () {},
];
This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I'm really sorry with the title, and im sorry if im asking this noob questions here, because i don't know what the keyword for this in google.
Ok, first i'm new to javascript and still learning this programming language. so i've seen this alot, but i don't know what it's mean
var myapp = function(){
var a = 'a';
var b = function(){
//some code goes here
}
return {
init: function(){
b();
//some code goes here
}
}
}() <-- what is it?;
So i've been wondering what this symbol '()' do at the end. and why many people writing a function inside a variable?
That means that the function is being exectued right after its declaration
On the other hand, declaring the function this way:
var myapp = function(){
lets you use it as an object..
Then you can use myapp.init() for example
This question already has answers here:
Why is this function wrapped in parentheses, followed by parentheses? [duplicate]
(6 answers)
Closed 8 years ago.
Question:
Why is the function wrapped in a parenthesis? I have taken this code out of parenthesis and it works with no trouble.
What is the benefit of having the code in a (function() { ...Code Here...})(); like it is in the following example?
Code:
(function() {
"use strict";
// Doesn't work because strict mode
// enforces the fact that the second example shouldn't work
if (true) {
function fn1() {
// This won't run
console.log("doesn't work -- have a great Die Hard Day XIII");
};
fn1();
}
})();
Code Here: What would sending the JQuery word as a parameter do for this namespace. I know that the reason that the function is enclosed in (...) is to create a namespace. I guess a better question would be as to why one would pass in a variable, but I would imagine that would be in case another namespace needed the variable.
( function( $ ) {
// Init Skrollr
var s = skrollr.init({
render: function(data) {
//Debugging - Log the current scroll position.
//console.log(data.curTop);
}
});
} )( jQuery );
I have taken this code out of parenthesis and it works with no trouble.
That’s not correct; it can’t run (or even be parsed) by itself. JavaScript sees function in a place where a function declaration can be and assumes it’s a function declaration. Parentheses are used to force the context to be an expression. The practice is redundant if it’s unambiguously a function literal – say, in a variable declaration – but many find it more readable. There’s a jsHint option to enforce it, for example.
Because then they are calling it:
(function() { ... })();
^^