I'm writing some JS at the moment but I'm not shure how to create a object in JS... On many sites i found many answers but which one should be use? For the first, I'm thinking there is no different. But i hope some of you can tell me more.
Here the three ways:
// Way 1
var SomeObject = {
foo: "bar",
bar: "foo",
someMethod: function() {
//code
}
};
// Way 2
var SomeObject = function() {
var self = this;
this.foo = "bar";
this.bar = "foo";
this.someMethod = function() {
//code
}
}
// Way 3
var SomeObject = function() {
var self = this;
this.foo = "bar";
this.bar = "foo";
}
SomeObject.prototype.someMethod = function() {
//Code
}
var obj = {}orvar obj = new Object()to give you some more possibilities