Should strong classes inherit from Object.prototype?

54 views
Skip to first unread message

Kevin Smith

unread,
Mar 11, 2015, 4:59:57 PM3/11/15
to streng...@googlegroups.com
It would seem beneficial to have strong classes not inherit from Object.prototype.
  • Having a weak object on the prototype chain apparently means that you can't ever completely flatten the prototype chain.
  • None of the methods on Object.prototype are actually useful, if you aren't doing conversions.
  • If you don't have "valueOf" and "toString", then you aren't supporting conversion to begin with.  I think?

Andreas Rossberg

unread,
Mar 12, 2015, 4:49:32 AM3/12/15
to Kevin Smith, streng...@googlegroups.com
One could do that, but what benefit do you see?

It's okay if you can't flatten all of the prototype chain. That will generally be the case, e.g. when you inherit from weak classes, which of course we need to support. You can still flatten any strong tail (or even subsequence) of the inheritance chain, which should give you all the performance benefits for invoking methods from those strong classes.

As for conversions, you might still want to invoke toString explicitly, for example. HasOwnProperty can also be useful (even though this reflection functionality should have been a static method on Object).

/Andreas

Youness Belfkih

unread,
Mar 26, 2016, 3:46:33 AM3/26/16
to Strengthen JS, zenpa...@gmail.com
custom Base classe for a code generators with no builtin __defineGetter__, __defineSetter__, __lookupGetter__, __lookupGetter__, toString, toLocalString, hasOwnProperty, constructor, isPrototypeOf, propertyIsEnumerable and toValue :D

Youness Belfkih

unread,
Mar 26, 2016, 4:02:37 AM3/26/16
to Strengthen JS, zenpa...@gmail.com
As for conversions, you might still want to invoke toString explicitly, for example. HasOwnProperty can also be useful (even though this reflection functionality should have been a static method on Object).

Thats the the problem they are supposed to be Reflection APIs but implemented on the base classe's prototype wich means every object will have them and that any code generator implmentor for say a C# compiler should try to work out the  differences between JavaScript builtins and C# built-ins ...

Besides any intent to inherit from Object would be explicit as in

class Base { ToString() { return `[[${this[Symbol.constructor].name}]]`; } }

when object's prototype is needed

class Normal extends Object { ...  }

i don't know but ever since Object.create was introduced i use it in my personal code to create clean object with no prototype, i use it instead of literals, maybe its a psychological relief for a developer to know that his objects and classes don't have unwanted useless methods and properties 

Le jeudi 12 mars 2015 08:49:32 UTC, Andreas Rossberg a écrit :
Reply all
Reply to author
Forward
0 new messages