Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why *not* "inherit"/extend from Object in JavaScript?

Tags:

javascript

It is well-known that declaring objects with JSON notation makes them "inherit" from (or, more precisely, to be built like) the base Object:

myobj={a:1, b:2};

which is nearly equivalent to myobj = Object.create(Object); myobj.a=1; myobj.b=2;

and than:

Object.getPrototypeOf(myobj)

prints the following:

Object
    __defineGetter__: function __defineGetter__() { [native code] }
    __defineSetter__: function __defineSetter__() { [native code] }
    __lookupGetter__: function __lookupGetter__() { [native code] }
    __lookupSetter__: function __lookupSetter__() { [native code] }
    constructor: function Object() { [native code] }
    hasOwnProperty: function hasOwnProperty() { [native code] }
    isPrototypeOf: function isPrototypeOf() { [native code] }
    propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
    toLocaleString: function toLocaleString() { [native code] }
    toString: function toString() { [native code] }
    valueOf: function valueOf() { [native code] }

However, one can call Object.create() while supplying null as an argument:

myobj2 = Object.create(null);

In this case empty prototype will be returned:

Object
    No Properties

And here goes the question: in which cases and why should I care to break the prototype chain to the original Object? Where it can be useful?

Update: as already corrected down below, I mean Object.create(Object.prototype) rather than Object.create(Object) that would return Function object (indeed Object() is a constructor function for Object prototypees).

like image 626
BreakPhreak Avatar asked Oct 06 '11 15:10

BreakPhreak


People also ask

How do you extend an object in JavaScript?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class. Syntax: class childclass extends parentclass {...}

Can objects inherit from other objects in JavaScript?

In JavaScript, an object can inherit properties of another object. The object from where the properties are inherited is called the prototype. In short, objects can inherit properties from other objects — the prototypes.

Does function inherit from object in JavaScript?

prototype is last in the prototype chain and it doesn't inherit from anything. The Object constructor is the one that inherits from Function.

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.


1 Answers

Instead of this:

myobj = Object.create(Object);

...I think you mean this is the equivalent:

myobj = Object.create(Object.prototype);

...because:

Object.getPrototypeOf( {a:1, b:2} ) === Object.prototype;  // true

As to why to use null early, if your object has no need for any of the properties of Object.prototype, ending the chain early would technically (though marginally) speed up property lookups when a property does not exist on the object in question.

Note that I say "early" because the chain always ends with null.

Object.getPrototypeOf( Object.prototype );  // null


             obj ----------> proto -------> Object.proto -----> null
         +---------+      +---------+      +-------------+     
         |         |      |         |      |             |
         |  foo:1  |      |  bar:1  |      | toString:fn |      null
         |         |      |         |      |             |
         +---------+      +---------+      +-------------+ 
              ^                ^                  ^               X
              |                |                  |               |
obj.foo ------+                |                  |               |
              ^                |                  |               |
obj.bar-------+----------------+                  |               |
              ^                ^                  |               |
obj.toString--+----------------+------------------+               |
              ^                ^                  ^               |
obj.baz-------+----------------+------------------+---------------+

   ^---property lookups

Notice that the baz property does not exist anywhere in the prototype chain.

Because of this, it needs to search each object in sequence until it finally reaches null before it realizes that baz doesn't exist anywhere.

If you eliminate the Object.prototype from the chain, it will get to null a little quicker.

like image 51
user113716 Avatar answered Dec 08 '22 01:12

user113716