Prototypal inheritance in JavaScript

Peter Lobsinger plobsing at gmail.com
Thu Jun 16 13:48:28 UTC 2011


On Thu, Jun 16, 2011 at 12:54 AM, rohit jangid <rohit.nsit08 at gmail.com> wrote:
> thanks peter,
> you were absolutely correct, when we use constructor's to create
> objects the constructor's prototype's properties are maintained by an
> inbuilt [[prototype]] link and not accessible through object.prototype
>
> while referring to the problem code
>
> ___________________________________________________
>
> var Constructor = function(){};
> Constructor.prototype.hello = "hello"
> Constructor.prototype.value = 1;
> Constructor.prototype.test = function() { console.log(this.hello); };
>
> // creating object from this constructor class
>
> var rj = new Constructor();    // create an instance of A
>
> rj.test();
> ____________________________________________________
>
> after reading some concept of internal [[prototype]] property , I was
> able to access the properties of "rj"
>
> to access the properties, we can use any of the three methods
>
> console.log(rj.constructor.prototype);

What happens if we change Constructor.prototype?

> console.log(Object.getPrototypeOf(rj));

This is part of ECMAScript 5. Not available in all browsers, but new
code on modern implementations should be using this.

> console.log(rj.__proto__);

This is a common, but non-standard extension to work around not having
access to [[prototype]] in ES3 and earlier. You may or may not want to
provide this in your implementation for reasons of compatibility, but
I would suggest you not make use of it in your own code. For example,
its use has been deprecated by Mozilla
(https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/Proto).

> all returns the same output
>
> { hello: 'hello', value: 1, test: [Function] }
> { hello: 'hello', value: 1, test: [Function] }
> { hello: 'hello', value: 1, test: [Function] }
>
> _____________________________________________________
>
> On Wed, Jun 15, 2011 at 10:58 AM, Peter Lobsinger <plobsing at gmail.com> wrote:
>> On Tue, Jun 14, 2011 at 8:57 AM, rohit jangid <rohit.nsit08 at gmail.com> wrote:
>>> thanks lucian.
>>>
>>> My view on inheritance was that.
>>> 1) first an empty object is created and it prototype points to
>>> constructor's prototype .
>>> 2) this object is passed into the constructor function where it can be
>>> refrenced by "this"
>>> 3) this is returned when we use new operator with constructor funtion .
>>>
>>> but as I tried to print what is stored in rj and rj.prototype , rj was
>>> empty object and rj.prototype was undefined. So how is it able to
>>> access that test() property .
>>
>> I think what you are missing is that object [[prototype]] (notation
>> used by ECMA-262 for internal properties) references are *implicit*
>> links to their constructor's (explicitly referenced) prototype
>> property. This means that rj.prototype need not bear any relation to
>> the [[prototype]] of rj as used for inheritance purposes. IIUC, there
>> is no mechanism in the standard to look up an object's [[prototype]]
>> from user code (although some implementations provide extensions to do
>> this).
>>
>> The core idea of this is documented in ECMA-262v3 section 4.2.1
>> ("Objects") complete with a nice diagram. It can be obtained at
>> http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm.
>>
>>> the problem came when , an object was using different properties and I
>>> couldn't figured out how it was accessing them and what are the other
>>> available properties.
>>> _______________________________________________
>>> http://lists.parrot.org/mailman/listinfo/parrot-dev
>>>
>>
>
>
>
> --
> Rohit Jangid
> Under Graduate Student,
> Deptt. of Computer Engineering
> NSIT, Delhi University, India
>


More information about the parrot-dev mailing list