使用null原型创建javascript对象,而不使用Object.create [英] Create javascript object with null prototype without using Object.create

查看:110
本文介绍了使用null原型创建javascript对象,而不使用Object.create的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种方法来创建一个JavaScript对象,其中 null prototype。

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

我不是允许使用 Object.create 函数。

我尝试实例化像 new这样的构造函数构造函数(),但返回的对象总是有一个非 null 原型,即使 Constructor.prototype === null

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

推荐答案

这样的对象已经存在,它是 Object.prototype ,所以你的代码很简单

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

x = Object.prototype

但你不能像这样创建一个新对象,因为 new 总是设置对象的原型:

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


如果Type(proto)不是Object,则将obj的[[Prototype]]内部属性设置为标准的内置Object原型对象,如15.2.4所述。 @ http://ecma-international.org/ecma-262/5.1 /#sec-13.2.2

你可以操纵 __ proto __ 直接

a = {}
a.__proto__ = null

这篇关于使用null原型创建javascript对象,而不使用Object.create的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆