【发布时间】:2016-07-07 03:25:34
【问题描述】:
在 ES5 之前有没有办法在没有原型的情况下创建对象?
即类似Object.create(null) (ES5)
我认为这样的事情可能会起作用,但最终语句意外返回true:
function withPrototype(p) {
function temp(){}
temp.prototype = p;
return new temp();
}
Object.getPrototypeOf(withPrototype(null)) === Object.prototype; // true
Object.getPrototypeOf 是 ES5。我在这里用它来展示。
【问题讨论】:
-
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 的 polyfill 说你只需要
Temp.prototype = null; -
没有。在 ES5 之前,只有 [[Prototype]]=null 的对象(可能不包括宿主对象和全局对象)是
Object.prototype。