开发者

how does an object knows about its parent in javascript

开发者 https://www.devze.com 2022-12-30 20:28 出处:网络
Lets suppose I made a class called Person. var Person = function(fname){this.fname = fname;}; pObj is the object I made from this class.

Lets suppose I made a class called Person.

var Person = function(fname){this.fname = fname;};

pObj is the object I made from this class.

var pObj = new Person('top');

now I add one pr开发者_高级运维operty to Person class, say lname.

Person.prototype.lname = "Thomsom";

now pObj.lname gets me "Thomson".

My question is that, when pObj didn't find the property lname in it, how does it know where to look for.


This has to do with how the javascript engine resolves references. It will start with the local Variable Object (bound to the scope) and then 'walk' up the prototype chain until it either finds it, or reaches the top.

You can read about this in detail here http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/


Every object has an internal property known as [[Prototype]] that carries a reference to another object, known as its prototype. When the JS interpreter is unable to find a named property in the object's own members, it looks for them in the object's prototype, then the prototype's prototype, and so on until it reaches Object.prototype, the lowest prototype of every object, which has no [[Prototype]] itself.

The [[Prototype]] property is assigned the value of the constructor-function's prototype property by the new operator. So when you call new Person the new object receives [[Prototype]]= Person.prototype. When you create a function, it gets a new, empty object for its prototype property, but you can reassign the constructor-function's .prototype completely as well as writing new members to it.

However the [[Prototype]] property remains the same through the life of the object; in particular, whilst adding new members to the Person.prototype makes them visible in all Person instances, assigning a new object to Person.prototype does not change the prototypes of existing Person instances.

Normally, [[Prototype]] is an invisible implementation detail. But in Mozilla, the [[Prototype]] internal property is exposed under the public __proto__ property. This non-standard extension has been adopted by Opera, Safari and Chrome, but not IE. In general it is considered poor form to rely on.

In ECMAScript Fifth Edition, you will be able to fetch the [[Prototype]] value using the new function Object.getPrototypeOf(person). Browser support is poor so far.


Every object has a prototype. When a properti is not set for object instance, it is looked in object's prototype. In Gecko and WebKit, you can use object's __proto__ property to get (or set) reference to its prototype.

You can get object prototype using standard myObj.constructor.prototype


It looks in its class's prototype.


The Person class has a prototype which is the inheritance chain for instances of type Person. Since that prototype has the lname property set then any instance of type Person, such as pObj will use the corresponding value unless the instance has overidden it, such as in pObj.lname = "Johnson".

0

精彩评论

暂无评论...
验证码 换一张
取 消