There seems to be quite a few techniques out there for creating client side objects. What I find really confusing is determining what each technique is called. I’ve used prototype before
MyObject.prototype.someMethod = function (e, a) {
.....
};
But I’ve also seen objects created in different like this
MyObject.someMethod = function () {
....
}
What is the second techinque called and why would one use one over the other?
The first example is analogues to defining methods on a class. Every instance of the class uses the same code;
thisin such a method points to the instance on which the method was invoked.The second is analogous to defining a static method on a class. You invoke the method like
MyObject.someMethod();It doesn’t make sense to invoke the method on an instance.
I guess you could call the first “prototype methods” and the second “constructor/class methods”.
If you are comfortable using classical OO terminology when talking about javascript, which is somewhat awkward as Javascript uses prototypal inheritance, you can call the the first “instance methods” and the second “static methods”.