JavaScript Function Declaration vs Object Literal [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript object creation using literals vs custom constructor functions
Function vs. Object Literal Notaion - Which is better, or is it a matter of preference?
Could anyone enlighten me what is the difference between below two ways of creating objects in JavaScript or more specifically drawbacks/advantages of them:
Function Foo() {
function bar () {...}
}
And
var Foo = {
bar : function() {}
}
Also since in latter way, there is no this keyword, how can I make it object instance member ?

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.

Javascript Functions and Objects Confusion [duplicate]

This question already has answers here:
Function and Object Javascript
(2 answers)
Closed 5 years ago.
In Javascript, we have two fundamental building blocks called functions and objects. But I'm a bit confused about the phrase functions are special type of objects. Anyways, in Javascript:
We create functions like this:
function foo(){}
Now the above declared function also behaves like an object as below:
foo.staticMethod = function(){}
Ok. I understand it.
Now similarly we create objects like this:
var obj = new Object() // Not using object literal here
That means, we need a function constructor Object to make even an empty object.
But Functions are also objects. How????
So my simple question is, if Object is used to create any new object, then how it can be an object itself as it accepts a property Object.prototype or I should say how a function can be an object ?
function Object(){
return {};
}

What is constructor? What kind of function object can be called constructor? [duplicate]

This question already has answers here:
Constructor function vs Factory functions
(8 answers)
Closed 5 years ago.
I'm learning javascript and I'm confused with this definition.
I look up in ECMA and it defines constructor as
function object that creates and initializes obejects.
So, can any function object be called constructor?
In js its common to call a function constructor if its aim is to be called with the new operator:
var me = new Human;//Human is a constructor
However, human languages are not that strictly defined, so you can probably use it always, you just need to have good arguments for your usage. A good arguable case:
function Animal(name){//actually rather a factory function
return {name};
}
var cat = new Animal("bob");//and now ? :/
So, can any function object be called constructor?
But not every function creates or initializes objects. Consider this example:
function add(a, b) {
return a + b;
}
This function only adds two values.
What is constructor? What kind of function object can be called constructor?
I'd argue that only functions that are intended to be called with new are supposed to be considered "constructors" (that includes functions created via class).
However, you can also create objects without calling a function with new:
function getPerson(name) {
return {name: name};
}
Whether or not you consider such functions constructors is probably subjective.

JavaScript - Difference beetween object's method named once and name twice [duplicate]

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.

Why people follow different approach to define function in javascript [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 8 years ago.
few years back people always define function like this way
1) function foo() {}
now i have noticed people define function like this way
2) var foo= function () {}
3) foo: function() {}
4) var GaugeBar = GaugeBar || {};
GaugeBar.generate = function (percentage) {}
so anyone JavaScript expert would tell me why people follow different approach for defining function? each signature has any special significance ?
when we should follow which one?
looking for good explanation. thanks
I think that there is some confusion in your question - the only valid approach to define a function is 1) and 2). The other two are just different uses of functions in objects.
I personally like to define named functions (function myFunc () {}) because if my program crashes the function names will appear in stack traces. However, there are situations where I use anonymous functions if those functions are not part of a public API or if they are used only once in my program.
Option 3) is basically a function that gets assigned to an object:
var myObject =
{ func: function () {}
, func2: function () {}
}
Option 4) is merely a way how to ensure that a variable contains an object and after that adding some functions to that object.
In practical terms, there is no difference whether you give your functions a name or you use anonymous functions (except for the stack traces and for differences already described in this SO question) and I believe it depends on the programmer's preferred code style. I would love if everyone named their publicly accessible functions as it makes debugging easier, but that's up to each developer to decide.

Categories