[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Differences between [[Class]] and constructor

ram

6/2/2015 6:02:00 PM

I have observed that the class ([[Class]]) name and the
constructor name of an object often is the same!

function className( object )
{ return 0,
Object.prototype.toString.call( object ).
replace( "\[object ", "" ).replace( "\]", "" ); }

let n = new Number( 3.14 );

className( n ) /* Number */

n.constructor.name /* Number */

So now, I am trying to find differences between the class
name and the constructor name.

The first difference I found is: You can change the
constructor name, but not the class name.

n.constructor = String /* function String() */

n.constructor.name /* "String" */

className( n ) /* "Number" */

Any other differences?

4 Answers

Andreas Bergmaier

6/2/2015 7:33:00 PM

0

Stefan Ram schrieb:
> I have observed that the class ([[Class]]) name and the
> constructor name of an object often is the same!

This might be the case for plain objects with the native constructors.
It is not for almost every other object, like

var p = new (function Person(){});
className(p) // "object"
p.constructor // Person

> The first difference I found is: You can change the
> constructor name, but not the class name.

Not exactly. You cannot change constructor names either, as the `.name`
property of functions is typically non-writable (if it exists at all, it
is non-standard, as opposed to [[Class]]). See
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Fun...
for more information.

What you can arbitrarily change is the `.constructor` property of any
normal object, there are no limitiations here.

Bergi

Thomas 'PointedEars' Lahn

6/2/2015 8:58:00 PM

0

Stefan Ram wrote:

> I have observed that the class ([[Class]]) name

That is _not_ the â??class nameâ?, it is the value of the internal â??[[Class]]â?
property of values. â??[[Class]]â? is purely a Specification mechanism and
does not need to be implemented in a way other than to satisfy the
algorithms of the Specification.

> and the constructor name of an object often is the same!

Often, but not always. As specified.

> function className( object )
> { return 0,
> Object.prototype.toString.call( object ).
> replace( "\[object ", "" ).replace( "\]", "" ); }

This is written shorter, less error-prone, more efficient, and actually
readable

(({}).toString.call(object).match(/\[object\s+(.+)\s*\]/) || [])[0]

> let n = new Number( 3.14 );

var n = Math.PI;

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

6/2/2015 9:06:00 PM

0

Andreas Bergmaier wrote:

> Stefan Ram schrieb:
>> I have observed that the class ([[Class]]) name and the
>> constructor name of an object often is the same!
>
> This might be the case for plain objects with the native constructors.
> It is not for almost every other object, like
>
> var p = new (function Person(){});
> className(p) // "object"

No, with this code it would be a capital â??Oâ?.

> p.constructor // Person
>
>> The first difference I found is: You can change the
>> constructor name, but not the class name.
>
> Not exactly. You cannot change constructor names either, as the `.name`
> property of functions is typically non-writable (if it exists at all, it
> is non-standard, as opposed to [[Class]]). See
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Fun...
> for more information.
>
> What you can arbitrarily change is the `.constructor` property of any
> normal object, there are no limitiations here.

Whereas â??normal objectâ? would be one whose prototype is not constructed
using (an emulation of) Object.create(â?¦), as I pointed out in an update of
my <http://stackoverflow.com/a/12593269/... today:

With a conforming implementation of Object.create(), following

WeatherWidget.prototype =
Object.create(Widget.prototype, {constructor: {value: WeatherWidget}});

the â??constructorâ? property of WeatherWidget instances is neither writable,
nor enumerable or configurable. Of those, an emulation of Object.create()
can only accomplish read-onlyness, see Function.prototype.extend() in
JSX:object.js.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

6/2/2015 9:08:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Stefan Ram wrote:
>> function className( object )
>> { return 0,
>> Object.prototype.toString.call( object ).
>> replace( "\[object ", "" ).replace( "\]", "" ); }
>
> This is written shorter, less error-prone, more efficient, and actually
> readable
>
> (({}).toString.call(object).match(/\[object\s+(.+)\s*\]/) || [])[0]

[1], of course. (So much for â??less error-proneâ? ;-))

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.