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

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.

Related

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

Javascript difference between store the function into variable with function name or without function name? [duplicate]

This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
What is the difference between these two functions;
var a = function () { console.log("hi"); }
var b = function xyz() { console.log("hi"); }
When i call these two functions both are giving same result. Then what is point in declaring function name 'xyz' ?
The second form is called a "named function expression". It gives you several benefits over normal function expressions:
It makes it a bit easier to debug -- when you have a bunch of functions like those, and you get an error, naming your functions makes it a bit easier to interpret the resulting stack trace.
You can call named functions recursively -- for example, doing:
f = function f() {
f();
};
f();
...works.
You can find more detailed information here.
Using named function expressions also comes with some disadvantages. For example, doing f = function g() { ... } will actually create two entities -- the functions f and g, which can potentially be confusing. (see comments) There are also some intricacies with scoping which can potentially cause bugs if you're not careful. You can find more detailed information here.

Access method using object in javascript [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 8 years ago.
I have a functions A B with same method in them. I want to access this method from another function. Depends upon some condition ill choose any one of version!
function A() {
var method = function() {
// do something..
}
}
function B() {
var method = function() {
// do something..
}
}
function other() {
// access method from function A() or B();
}
is there any way to access using objects like we do in JAVA or any other way?
You could technically make "method" global by putting it on the window global object:
function A() {
window.method = function() {
// do something..
}
}
function other() {
// access window.method
}
Generally, this isn't what you want to do. If you find yourself having to do weird hacks like this to get access to a method it usually means something wrong. You should try to pull the method out into a shared scope where it's accessible where it's needed.

Different ways of naming functions? [duplicate]

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.

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