Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create javascript object with null prototype without using Object.create

I need to find a way to create a JavaScript object with null prototype.

I'm not allowed to use the Object.create function.

I tried instantiating a constructor like new Constructor(), but the returned object always has a non-null prototype, even if Constructor.prototype === null.

like image 323
fgfjhgrjr erjhm Avatar asked Feb 13 '26 01:02

fgfjhgrjr erjhm


1 Answers

Such an object already exists, it's Object.prototype, so your code is as simple as

x = Object.prototype

but you cannot create a new object like this, because new always sets the proto to an object:

If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4. @ http://ecma-international.org/ecma-262/5.1/#sec-13.2.2

You can manipulate __proto__ directly

a = {}
a.__proto__ = null
like image 172
georg Avatar answered Feb 15 '26 15:02

georg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!