2 ms·
It's a bit sad that one of the most useful cases for `Object.create` was not covered: it is the only way to create an object without a prototype. Using construc
by masklinn 14y ago
It's a bit sad that one of the most useful cases for `Object.create` was not covered: it is the only way to create an object without a prototype. Using constructor functions, even if you explicitly set the ``prototype`` to ``null``, instances's ``__proto__`` remains ``Object.prototype``:
> var F = function () {};
> F.prototype = null;
null
> (new F).__proto__ === Object.prototype;
true
This means all the "default" object methods are there (e.g. #hasOwnProperty) and ready to get clobbered with all the risk it entails[0].
With ``Object.create(null)``, however, the __proto__ of the created object is effectively ``null``:
> Object.create(null).__proto__
null
and such pesky methods as #hasOwnProperty or #toString are not there for clobbering.
And an issue of Object.create failed to be mentioned as well: you can't use ``instanceof`` directly, you have to create one-off "constructor functions" to call ``instanceof`` on them.
[0] http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/ http://www.devthought.com/2012/01/18/an-object-is-not-a-hash...