tags:

views:

82

answers:

5

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 property 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.

A: 

It looks in its class's prototype.

Marcelo Cantos
how does it know who is its class....
alter
This is baked into language. You attached lname to the `property` attribute of `Person`, which is the function you defined to create the object.
Marcelo Cantos
A: 

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

el.pescado
__proto__ is not defined in the ES3 standard, so to say that every object has one is to simplify things :)
Sean Kinsey
You're right, `__proto__` is non-standard.
el.pescado
which version of Javascript supports _proto_
alter
el.pescado
+1  A: 

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/

Sean Kinsey
The prototype chain isn't the scope chain. Scopes should not be affected by prototypes at all. (Though they are a little bit on the awful Blackberry browser due to a bug detailed on that site.)
bobince
@bobince Did my answer lead you to believe that I thought they were the same? The prototype chain will be walked whenever the part left of the '.' operator is an object, else the scope chain.
Sean Kinsey
but every time part left of the '.' will be an object...isn't it?...or do you mean an instance of the class by the object
alter
What I meant to say (in a rather clumsy way) was that whenever the reference is placed to the right of a '.' operator, the prototype chain is walked, else the scope chain (that is, when there is no . to the left).
Sean Kinsey
ok. but how does it know that who's prototype to look into...I want to know that where does an object stores information about its parent.
alter
there is no such thing as `parent` :) All objects in javascript has a prototype (its 'template'), when using different techniques to subclass you normally modify this chain manually so as to let subclasses have access to methods and members on its baseclass's prototype.
Sean Kinsey
yes I understood that objects are referring the their baseclass's prototype so that everytime a property gets added to this prototype, it gets available to all the objects. But what I am not clear with is, where is this reference stored? Where does my object stores a pointer to its base class pointer?
alter
ok got it....its stored in p.__proto__
alter
No, its stored in .prototype :) `__proto__` is an extension outside the ECMAScript standard.
Sean Kinsey
A: 

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".

maerics
+1  A: 

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.

bobince
@bobince....few doubts.1) Did you mean that pObj = new Person('top') adds a property called "Prototype" in pObj which is a reference to Person.Prototype?2) Is there any way in which I can interact with _proto_. May be by augmenting it further?...or its read only!!
alter
`pObj= new Person('top')` creates a new object and sets its internal `[[Prototype]]` property to the object referred to the visible `Person.prototype` property. You can't see the `[[Prototype]]` property except as noted above in browser-specific ways, but you can still interact with `Person.prototype`, adding new methods there which will then appear on `pObj`.
bobince