Code 1:
var Something = {
name: "Name",
sayHi: function(){
alert(Something.name);
}
}
Code 2:
function Something(){
this.name = "Name";
}
Something.prototype.sayHi = function(){
alert(Something.name);
}
Edit: So, Guys, you mean the Second one is better? or more "formal" ?
Code 1:
var Something = {
name: "Name",
sayHi: function(){
alert(Something.name);
}
}
Code 2:
function Something(){
this.name = "Name";
}
Something.prototype.sayHi = function(){
alert(Something.name);
}
Edit: So, Guys, you mean the Second one is better? or more "formal" ?
Share Improve this question edited Aug 8, 2009 at 6:55 DNB5brims asked Aug 8, 2009 at 6:38 DNB5brimsDNB5brims 30.6k50 gold badges134 silver badges204 bronze badges 2- This is also a good question for me, thanks for posting. – Alix Axel Commented Aug 8, 2009 at 7:02
-
In regards to your question in the edit, it depends on what you want to do. If you only have one copy of the object, go for the first syntax. If want to create multiple copies (like if
Something
was aClass
in other languages), go for the second syntax (or actually, an edited version in which you definesayHi
inside the constructor usingthis.sayHi = function() { ... }
, this is the norm). – Sinan Taifour Commented Aug 8, 2009 at 7:07
3 Answers
Reset to default 8Basically in the first example you declare an object literal which is actually already an object instance.
In your second example, you define a constructor function which can be used with the new
operator to create object instances.
Object literals can be also used to create new instances of objects and do prototypal inheritance, Douglas Crockford promotes also this technique.
Basically you can have an object
operator:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
This helper function can be used in a very intuitive and convenient way.
It basically receive an object as a parameter, inside the function a new object instance is created, the old object is chained to the new object's prototype, and its returned.
It can be used like this:
var oldObject = {
firstMethod: function () { alert('first'); },
secondMethod: function () { alert('second'); },
};
var newObject = object(oldObject);
newObject.thirdMethod = function () { alert('third'); };
var otherObject = object(newObject);
otherObject.firstMethod();
You can go further as you want, making new instances from the previously defined objects.
Remended :
- Prototypal Inheritance in JavaScript
- Advanced JavaScript (Video)
In the first code snippet, Something
is a simple object, and not a constructor. In particular, you cannot call:
var o = new Something();
Such a form of creating objects is perfect for singletons; objects of which you only need one instance.
In the second snippet, Something
is a constructor, and you can use the new
keyword with it.
Edit:
Also, in your second snippet, since you are using Something.name
as opposed to this.name
, it will always alert the name of the constructor itself, which is "Something", unless you override that property with something like Something.name = "Cool";
.
You probably wanted that line to say:
alert(this.name);
In first example you can do
Something.sayHi();
while in the second one you have to do it
new Something().sayHi();