I've come across another developer's code which does something like this to define a Javascript type:
function Ninja() {}
Ninja.prototype = {
swingSword: function(){
return true;
}
}
when the more conventional manner is something like:
function Ninja() {}
Ninja.prototype.swingSword = function() {
return true;
}
The first form mostly works, in that you can create a new instance and call the swingSword
method, and instanceof
is accurate:
var ninja = new Ninja();
ninja.swingSword(); // returns true
ninja instanceof Ninja; // also true
However, ninja.constructor
points to Object
rather than the Ninja
function. This is an issue for me: I'm trying to access the constructor via an instance, as a workaround for the constructor not being exposed in any other way. But apart from accessing the constructor
attribute for reflection/workaround hacks, does the first form cause any significant issues?
Are there any other important attributes of the original Ninja.prototype
which might be getting hidden? Will the first form cause any issues with inheriting types, or common libraries?
I'm trying to figure out if this an unusual but acceptable Javascript pattern, or actually a common Javascript misstep or "gotcha" and should be considered a bug.
Note my Ninja
example is based on some example code from a tutorial by John Resig. The actual type in question is not called Ninja
, and is longer and more complicated.
Also, I've read this guide to constructors and prototypes and understand, eg how prototypes are used in attribute lookups and why isinstanceof
still works.