It is often said that every Javascript object has a prototype
property, but I find that foo.prototype
has a value only if foo
is a function.
On Chrome and Firefox, obj.__proto__
has a value -- is this the said prototype
property? But on IE 9, it won't work (is there some way that can?), and I thought by prototype
property, that means obj.prototype
should work?
I understand that Object.getPrototypeOf(obj)
seems to show this prototype
property, but why need a special method to get it? Why not just like person.name
, which is to get the name
property of the person
object?
Update: by the way, obj.constructor.prototype
seems to sometimes be that prototype, but sometimes not, as in the following code done with Prototypal inheritance with no constructor: (this method is in the Pro Javascript Design Patterns book by Harmes and Diaz by Apress 2008, p. 46)
var Person = {
name: 'default value',
getName: function() {
return this.name;
}
}
var reader = clone(Person);
console.log(reader.getName());
reader.name = "Ang Lee";
console.log(reader.getName());
function clone(obj) {
function F() {};
F.prototype = obj;
return new F;
}
console.log("the prototype of reader is", Object.getPrototypeOf(reader));
console.log(Object.getPrototypeOf(reader) === reader.constructor.prototype);
console.log(Object.getPrototypeOf(reader) == reader.constructor.prototype);
console.log(Object.getPrototypeOf(reader) === reader.__proto__);
console.log(Object.getPrototypeOf(reader) == reader.__proto__);
the result will show false, false, true, true for the last 4 lines.
Each and every JavaScript function will have a prototype property which is of the object type. You can define your own properties under prototype . When you will use the function as a constructor function, all the instances of it will inherit properties from the prototype object.
A Function object's prototype property is used when the function is used as a constructor with the new operator. It will become the new object's prototype. Note: Not all Function objects have the prototype property — see description.
All objects have prototypes, except for the base object. The base object is the object created by the user, or an object that is created using the new keyword. The base object has access to some methods and properties, such as . toString.
Both objectOne and objectTwo are non-function objects therefore they don't have a prototype property.
Every JavaScript object has an internal "prototype" property, often called [[prototype]], which points to the object from which it directly inherits. This is exposed in FF and Chrome by the non-standard __proto__
property. Object.getPrototypeOf
is a getter for this internal property.
Every JavaScript function [object] has a property prototype
, which is initialized with an [nearly] empty object. When you create a new instance of this function by calling it as a constructor, the [[prototype]] of that new object will point to the constructor's prototype
object.
If you get the [[prototype]] of a function (every function is an object, so it has one), it will result in the Function.prototype
object from which functions inherit their methods (like bind, call, apply etc). See also Why functions prototype is chained repeatedly? on that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With