Different ways of naming functions? [duplicate] - javascript

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
In Javascript what is the difference between:
var name = function() { //stuff to do };
{name : function() { //stuff to do } };
function name() { //stuff to do };

As written by Stoyan Stefanov in "JavaScript Patterns":
In function declarations and named function expressions, the name
property is defined. In anonymous function expressions, it depends on
the implementation; it could be undefined (IE) or defined with an
empty string (Firefox, WebKit):
function foo() {} // declaration
var bar = function () {}; // expression
var baz = function baz() {}; // named expression
foo.name; // "foo"
bar.name; // ""
baz.name; // "baz"
The name property is useful when debugging code in Firebug or other
debuggers. When the debugger needs to show you an error in a function,
it can check for the presence of the name property and use it as an
indicator. The name property is also used to call the same function
recursively from within itself. If you were not interested in these
two cases, then an unnamed function expression would be easier and
less verbose.
The case against function declarations and the reason to prefer
function expressions is that the expressions highlight that functions
are objects like all other objects and not some special language
construct.

Related

JS - Defining prototype methods [duplicate]

This question already has answers here:
Defining a Javascript prototype
(5 answers)
Closed 5 years ago.
Is there a difference between this syntax
function Foo() {}
Foo.prototype.method1 = function() {};
Foo.prototype.method2 = function() {};
and this one?
function Foo() {}
Foo.prototype = {
method1: function() {},
method2: function() {}
}
Should one be prefered to the other?
There is a slight difference between those two options. I would recommend using the former to preserve the constructor property pointing to the actual constructor function used to create the object. In the following example you'll see the difference under the hood of both options:
Foo used the prototype.newMethod syntax.
Bar used the prototype = {...} syntax.
In case you'd like to use the Bar syntax, you can always set the constructor property to the correct function.
I hope this helps, let me know if you have any doubts.

Anonymous vs Named Functions in JavaScript Module Pattern? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 6 years ago.
Is there any benefit for the following JavaScript module defenition:
var module = (function(){
var PublicFnc = function(){ // variable declaration
alert('hi');
}
return {
f : PublicFnc
}
})();
module.f();
Over the following:
var module = (function(){
function PublicFnc(){ // function declaration
alert('hi');
}
return {
f : PublicFnc
}
})();
module.f();
Although the second example is more convenient since it is more similar to Java/C# the anonymous methods are used more often and I'm wondering what is the benefit?
#Alexander, thanks for marking the question as duplicate but I tend to leave it open since I'm asking for the benefits within the context of module patterns and not generally
One of the differences between them can be explained using a concept called hoisting
In case:
a(); //you cannot call this function before its definition as here var a is undefined
var a = function(){ //function statement
console.log("Hello World");
}
a(); //this will call the function even before its definition because of hoisting
function a(){ //function definition
console.log("Hello World");
}
Also some the function in the above case is assigned a memory space using that variable.
In hoisting, function definition is hoisted and not the function statement.
Read more about hoisting here http://www.w3schools.com/js/js_hoisting.asp
Also, when to use statement and when to use definition depends on use case and personal preference

When will I use this kind of function declaration? [duplicate]

This question already has answers here:
Javascript functions like "var foo = function bar() ..."?
(9 answers)
Closed 6 years ago.
I saw this kind of function declaration many times when reading other peoples's code.
var foo = function bar() {
console.log("Some text");
};
And I got this result.
foo(); // "Some text"
bar(); // Uncaught ReferenceError: bar is not defined(…)
Since bar stays undefined, I wonder what the purpose of writing bar there and the difference if I write it with omitting bar. However, I wonder if there is a certain condition or a reason why people write function like that.
When will I write function like that?
When you write var foo = function() {} you declare a variable named foo that has an anonymous function (function that has no name).
When you write var foo = function bar() {} you declare a variable named foo that has a function named bar. This is useful for debugging. When you have an error, it will show that it's in function bar().
Since function names are only available inside the function, calling bar() throws an error.

JavaScript myFunction : function(int x) vs function myFunction(int x)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between a function expression vs declaration in Javascript?
What is the difference between the two ways to declare a function in JavaScript?
myFunction : function(variable)
{
}
or
function myFunction(variable)
{
}
Your first code snippet is not valid - it only works within an object; example:
var object = {
myFunction: function(variable) { }
};
// object.myFunction();
Basically there are two ways to define a function ins JavaScript:
function myFunction(variable) { }
var myFunction = function(variable) { };
The difference is: The first type of declaration uses the function statement and therefore allows you to use the function before it has been declared. Example:
console.log(myFunction()); // prints test
function myFunction(variable) { return "test"; }
Read more about it here.
This is not possible with the second type of function declaration, which assigns an anonymous function to a variable. The function can't be used before the variable has been declared.
One is a method. The other a function.
Functions are defined
function myfunction() {..}
Methods are defined
myobject.mymethod = function() {...} ;
A method is a property of an object that points to / is a function of that object
Really it depends on how you structure your objects. Functions are usually used in global libraries that are non object specific while methods are tied to objects to execute specific pieces of functionality.

could you explain the function in javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does this mean? (function (x,y)){…}){a,b); in JavaScript
(function(){
var foo = 'Hello world';
})();
i don't know what's use of it? and what's meaning of it/?
On its own it does nothing except declare a variable that isn't used - it should invoke some other functions to do something useful.
That said, what you have is an immediately invoked function expression, i.e. an anonymous function:
function() { ... }
which is invoked with no parameters:
(f....)();
The rationale is two fold:
it allows the function to be defined and called without giving it a name in the global name space
any variables defined within the function are also held within that scope, and don't pollute the global name space.
It's an anonymous function that executes immediately.
The idea is to create a private scope. Often one would return a closure from the anonymous function that retains access to variables created in that scope.
For example
var greet = (function () {
var foo = 'Hello world';
return function () {
alert(foo);
}
}());
greet();
This calls an anonymous function immediately.
Have a look here: What does this “(function(){});”, a function inside brackets, mean in javascript ?

Categories