Code 1:
var Something = {
name: \"Name\",
sayHi: function(){
alert(Something.name);
}
}
Code 2:
function Somethin
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);