Object.getConstructorOf();

Rick Waldron waldron.rick at gmail.com
Sun Sep 21 15:18:24 PDT 2014


On Sunday, September 21, 2014, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> I see `Object.getConstructorOf(x)` rather a wrap of
> `Object.getPrototypeOf(x).constructor` where in a string version, which I
> agree with Brendan is kind of misleading anyway, would
> be `Object.getPrototypeOf(x).constructor.name`
>
> ```js
> Object.getConstructorOf = function (x) {
>   return Object.getPrototypeOf(x).constructor.name || 'Object';
> };
> ```
>
> Now, how would this play with people still using anonymous expressions to
> create constructor is a mystery, since `var A = function(){};` won't ever
> have a name
>

This is not correct. Incidentally, there was discussion in the last hour on
traceur about this, here is my response:

https://github.com/google/traceur-compiler/pull/1365#discussion_r17828244

Which cites:
https://people.mozilla.org/%7Ejorendorff/es6-draft.html#sec-variable-statement-runtime-semantics-evaluation

Look under *VariableDeclaration* : *BindingIdentifier**Initializer*, step
4.c.i


Rick










>  so we are back to a more meaningful
> `Object.getPrototypeOf(x).constructor` instead of a string where again
> primitive will mostly fails since ambiguous 'cause indeed `new String(123)
> !== String(123)` but both have the same constructor.
>
> This leads to the following approach:
>
> ```js
> Object.getConstructorOf = function (x) {
>   return x instanceof Object ?
>     Object.getPrototypeOf(x).constructor : null;
> };
> ```
>
> Now we have jut another problem, people still defining prototypes
> reassigning, instead of enriching, and forgetting the constructor:
>
> ```js
> function A() {}
> A.prototype = {
>   method: function () {
>   }
> };
> ```
>
> Where is the constructor ? .... this trip to end up realizing that the
> constructor means actually nothing in JS world if not as generic implicit
> method from user land, and complete black box alchemy in native land.
>
> Best Regards
>
>
>
> On Sat, Sep 20, 2014 at 8:12 PM, Brendan Eich <brendan at mozilla.org
> <javascript:_e(%7B%7D,'cvml','brendan at mozilla.org');>> wrote:
>
>> L2L 2L wrote:
>>
>>> I see the value of this, cause it denote not only the constructor, but
>>> what an object is:
>>>
>>> Object.getConstructorOf("")//String
>>> Object.getConstructorOf(0)//Number
>>> Object.getConstructorOf(function(){})//Function
>>> Object.getConstructorOf({})//Object
>>> Object.getConstructorOf([]);//Array
>>>
>>> If you try the first to with Object.getPrototypeOf("");
>>> Object.getPrototypeOf(0);
>>>
>>> You'll get an error message, cause primitive value don't have prototype.
>>> But they do have wrapper objects; constructor objects.
>>>
>>> This to me is a needed feature of the language.
>>>
>>
>> It's not a "kernel language" feature, as you showed -- it is library code
>> that can be built on top of the core language. And you are right that we
>> seem to observe a fair amount of
>>
>> Object.prototype.toString.call(x).slice(8, -1)
>>
>> for value x.
>>
>> However, getConstructorOf("") returning "String" (a string value),
>> instead of the constructor function, is misleading. But returning the
>> constructor function reference is wrong for primitive values, because 42
>> !== new (42).constructor(42). Primitives have value not reference semantics.
>>
>> What you are really proposing is an Object.getToStringTag method. But in
>> ES6, that is just x[Symbol.toStringTag], if I'm not mistaken. So do we
>> really need Object.getToStringTag(x), which is longer?
>>
>> See http://people.mozilla.org/~jorendorff/es6-draft.html#sec-
>> object.prototype.tostring for the ES6 spec.
>>
>> Then you could argue that an Object "static method" is better both to
>> hide the Symbol.toStringTag detail and to enable polyfilling on pre-ES6
>> browsers. That's a fair point.
>>
>> /be
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> <javascript:_e(%7B%7D,'cvml','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/20140921/2a183f9f/attachment.html>


More information about the es-discuss mailing list