This draft note is the result of pondering on this a bit more. This pondering also resulted in some clarification (for me) of how the
instanceof operator seems to work, and how its utility is doubtful.
When a property named
foo is requested from an object
obj.foo, first the set of properties for object itself is searched for the property. If it is not found, then
obj.__proto__ is searched for a property named
foo. If not found there, then it searches
obj.__proto__.__proto__, continuing up the "prototype chain" until it either finds the property or
obj.__proto__.__proto__....__proto__ is null, and
undefined is returned.
Again: the name of the property for the prototype of an object is not "prototype": its name is "__proto__".
That is one part of the potential confusion.
fhas a property named "prototype" - which is not its prototype
This is another part of the potential confusion.
f.prototypeis not the function's
As for any object,
f.__proto__ is the function's prototype.
f can be used to generate new objects via
newObject = new f(args).
When used this way,
f is sometimes called the "constructor" for
When an object is created from a function
newObject = new f(args), the new object's prototype
property is set to the property named "prototype" of
newObject.__proto__ = f.prototype .
That is what
f.prototype is used for.
Note that there are other ways to create an object with a specified prototype, and in fact these other methods are recommended by some folks, avoiding the "new" keyword altogether.
While every function can be used as a constructor, there is also a property named "constructor" for some objects.
In particular, for any function
f.prototype.constructor = f.
Since objects created from
new f(args) have their "__proto__" property initialized to
f.prototype, this means that for any object
newObject created via
newObject = new f(args) for some function
newObject.constructor will refer to the function used to create the object. Initially, at least, as you can change the
Other than a record of the object's ancestry that might be useful in a handful of cases (that I've never encountered), I don't know how the knowledge of the constructor would be generally useful.
instanceof operator that returns true or false, and can be called like so:
obj instanceof f
You can only use a function on the right side:
instanceof will throw an error if
f is not a function.
The Mozilla Developer network summarizes this operator quite succinctly (I have used "function" instead of "constructor", since every function can be a constructor):
The instanceof operator tests whether an object has in its prototype chain the prototype property of a given function.
This is illustrated in the figure below.
f) is true if
f.prototypeis equal to
__proto__in the prototype chain of
Answer: I think few do.
There are also problems with trying to use
instanceof across iframes.
Crockford has remarked (in 2003) about the useless instanceof operator (link via kangax's article).
In particular, you can muck with the prototype and constructor properties after-the-fact, and this would likely invalidate the notes above in whatever curious ways you (or the code you are using) has mucked with things.