Is there any difference between the below declarations?
declaration 1:
someName.test1 = function() {alert("test1")}
declaration 2:
someName.protoype.test2 = function() {alert("test2")}
Duplicate of Difference Between Class Properties and Function Prototype in Javascript
Yes, there’s a difference:
adds this function to the object (or class)
someName.adds this function to all instances of class
someNameFor a real example, look at this:
which is just a function namespaced into the
Objectclass, taking the object of interest as its first parameter, whereas if it had been written:Then every
Objectwould have that method and it would be used like this:[but don’t do that, since adding methods to the
Objectprototype is frowned upon!]