Reputation: 1511
Check out the image. How can this be? Aren't objects supposed to inherit its prototype's methods?
King <-- FixedMovementPiece <-- Piece
Piece has the setXY method.
Upvotes: 3
Views: 84
Reputation: 27277
__proto__
(defined in most current browsers but not in the current ECMAScript specification) is what gets used when the prototype chain is being searched through.
prototype
is used when a function is called as a constructor, to assign the __proto__
property of the new object. As prototypes are typically not constructors, prototype.prototype
is rarely useful or even defined.
examples:
Array.prototype === (new Array()).__proto__ //true
(new Array()).prototype === undefined //true
var a = {0:'a', 1:'b', 2:'c', length:3}
a.toString() // "[object Object]"
var a = {0:'a', 1:'b', 2:'c', length:3, __proto__:Array.prototype}
a.toString() // "a,b,c"
var obj = {__proto__:{}}
obj.name // undefined
obj.__proto__.name = "someString"
obj.name // "someString"
obj.name2 = "anotherString"
obj.__proto__.name2 // undefined
Upvotes: 2
Reputation: 147363
Objects inherit from their constructor's prototype (i.e. the one the constructor had when the the instance was created), which is referenced by an internal [[Prototype]]
property.
Only functions have a prototype property by default. e.g.
// Declare function
function Foo(name) [
this.name = name;
}
// Add a showName method to its prototype
Foo.prototype.showName = function() {
return this.name;
}
// Create an instance
var foo = new Foo('foo');
// Call inherited method
foo.showName(); // foo
There is also a non standard __proto__
property in Mozilla browsers that references an object's [[Prototype]]
, it may be in ES6 but it is not suitable for the general web.
Upvotes: 1