Extended Object Literals to review

Juan Ignacio Dopazo dopazo.juan at gmail.com
Mon Mar 14 14:13:44 PDT 2011


>
> I don't see how I can recover `s` from `o` (or `c`), which seems like a
> useful operation.  What am I missing?


I totally forgot about Object.getPrototypeOf(). I definitely need to play a
little more with ES5. Try this:

function S() {}
function C() {}
C.prototype = Object.create(S.prototype, {'constructor':{value:C}});

var o = new C();

var proto = Object.getPrototypeOf(o);
while (proto) {
  console.log(proto, proto.constructor);
  proto = Object.getPrototypeOf(proto);
}

Juan

On Mon, Mar 14, 2011 at 5:34 PM, P T Withington <ptw at pobox.com> wrote:

> On 2011-03-14, at 14:29, Jorge wrote:
>
> > On 14/03/2011, at 18:32, P T Withington wrote:
> >> On 2011-03-13, at 18:15, Juan Ignacio Dopazo wrote:
> >>>
> >>>
> >>> The idea behind it was to be able to walk down the prototype chain by
> doing
> >>> o.constructor.prototype.contructor.proto... But then I realized that's
> not
> >>> the case even in today's javascript.
> >>
> >> I agree that we need this.  (...)
> >
> > Isn't that the purpose of Object.getPrototypeOf() ?
>
> If I have a class `c` whose superclass is `s`, I'm trying to understand how
> I can get from an object that is an instance of `c` to the superclass.  I
> believe the current desugaring that Alan proposed, which obeys Brendan's
> recommendation that `constructor` be a property of the prototype results in:
>
>  o = new c();
>  o.constructor === c
>  o instanceof c => true
>  o instanceof s => true
>  o.constructor.prototype instanceof s => true
>
> but:
>
>  o.constructor.prototype.constructor === c
>
> ?
>
> I don't see how I can recover `s` from `o` (or `c`), which seems like a
> useful operation.  What am I missing?
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110314/1d1e5d5e/attachment-0001.html>


More information about the es-discuss mailing list