正如@Oriol 所示,没有“官方”(符合规范)的方式来做到这一点。
然而,确实有一个没有原型的物体——Object.prototype
它本身。
15.2.4 对象原型对象的属性
[[Prototype]]
Object 原型对象的内部属性null
值为 ,内部[[Class]]
属性值为"Object"
。
您可以通过实例化一个新环境(ES6 术语中的“领域”)来潜在地“创建”这样的对象,例如通过 an <iframe>
,捕获它的Object.prototype
,剥离它的属性,瞧——你得到了一个新的空对象。
function getNoProtoObject(callback) {
var iframe = document.createElement('iframe');
iframe.onload = function() {
var obj = iframe.contentWindow.Object.prototype;
document.body.removeChild(iframe);
// Remove all built-in enumerable properties.
for (var name in obj) {
delete obj[name];
}
// Remove known built-in non-enumerable properties, which may vary.
delete obj['constructor'];
delete obj['hasOwnProperty'];
delete obj['isPrototypeOf'];
delete obj['propertyIsEnumerable'];
delete obj['toLocaleString'];
delete obj['toString'];
delete obj['toSource'];
delete obj['valueOf'];
delete obj['watch'];
delete obj['unwatch'];
delete obj['__defineGetter__'];
delete obj['__defineSetter__'];
delete obj['__lookupGetter__'];
delete obj['__lookupSetter__'];
delete obj['__proto__'];
callback(obj);
};
iframe.src = 'about:blank';
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
getNoProtoObject(function(o) {
console.log(o); // Object { }
console.log(Object.getPrototypeOf(o)); // null
console.log(o.__proto__); // undefined
});