Extended Object Literals to review

Juan Ignacio Dopazo dopazo.juan at gmail.com
Sun Mar 13 15:15:16 PDT 2011


@Brendan My mistake was thinking that setting enumerable to false also made
it return false on hasOwnProperty().

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.

Maybe adding a 'superclass' property to the constructor could be useful? As
in...

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

Juan

On Sun, Mar 13, 2011 at 6:16 PM, Brendan Eich <brendan at mozilla.com> wrote:

> On Mar 12, 2011, at 6:05 PM, Juan Ignacio Dopazo wrote:
>
> Right, my bad then.
>
>
> Maybe not, it's a difference but possibly someone will make the case for
> it. I still favor putting 'constructor' on the class prototype (constructor
> function .prototype in plain old ES5). It is more efficient and usually
> per-instance is neither wanted for some "hasOwnProperty('constructor') must
> return true" reason (presumably to prevent shadowing).
>
> If one wants higher integrity, Object.defineProperty(C.prototype,
> 'constructor', {value:C, writable:false, configurable:false}} does the trick
> (that is verbose on purpose to be clear, there's a much shorter way to say
> it, using the default undefined->false conversions for missing attributes).
>
> Allen, did you want 'constructor' to be in the class prototype?
>
> /be
>
>
> Juan
>
> On Sat, Mar 12, 2011 at 8:37 PM, Brendan Eich <brendan at mozilla.com> wrote:
>
>> On Mar 12, 2011, at 2:58 PM, Juan Ignacio Dopazo wrote:
>>
>> > Correct me if I'm wrong, but I think it should be like this:
>> >
>> > function S() {}
>> >
>> > function C() {
>> >       Object.defineProperty(this, 'constructor', {
>> >               value: C,
>> >               enumerable: false,
>> >               writable: false,
>> >               configurable: false
>> >       });
>> > }
>> > C.prototype = Object.create(S.prototype);
>> >
>> > var o = new C();
>> > console.log(o.constructor == C); // true
>> > console.log(o.constructor.prototype.constructor == S); // true
>>
>> But this also puts an own property on every instance, which is not how
>> .constructor works with functions:
>>
>> js> function C(x) { this.x = x; }
>> js> c = new C(42)
>> ({x:42})
>> js> c.hasOwnProperty('x')
>> true
>> js> c.hasOwnProperty('constructor')
>> false
>> js> c.constructor
>> function C(x) {this.x = x;}
>> js> c.constructor === C
>> true
>>
>> /be
>>
>> >
>> > Juan
>> >
>> >
>> > On Sat, Mar 12, 2011 at 4:36 PM, Allen Wirfs-Brock <
>> allen at wirfs-brock.com> wrote:
>> > good point, the desugaring should be:
>> >
>> > function c() {};
>> > c.prototype=Object.create(s,protoype,{'constructor': { value:c,
>> enumerable: false, writable: false, configurable:false}});
>> >
>> > We can debate about the attributes of the constructor (and the
>> constructor's prototype) property but my stake in the ground is that these
>> should be frozen because they are defined declaratively.
>> >
>> > On Mar 12, 2011, at 11:06 AM, Michael Haufe wrote:
>> >
>> >> On Sat, Mar 12, 2011 at 11:02 AM, Allen Wirfs-Brock <
>> allen at wirfs-brock.com> wrote:
>> >> [...]
>> >> > class c {
>> >> >    <proto: s.prototype>
>> >> > }
>> >>
>> >> > class c {
>> >> >    <superclass: s>
>> >> > }
>> >>
>> >> > both are equivalent to:
>> >>
>> >> > function c() {};
>> >> > c.prototype=Object.create(s.prototype);
>> >> [...]
>> >>
>> >> So if "var b = new c", then "b.constructor" will be "s" instead of "c"?
>> >>
>> >>
>> >
>> >
>> > _______________________________________________
>> > es-discuss mailing list
>> > es-discuss at mozilla.org
>> > https://mail.mozilla.org/listinfo/es-discuss
>> >
>> >
>> > _______________________________________________
>> > 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/20110313/739215d7/attachment-0001.html>


More information about the es-discuss mailing list