javascript - 为什么由 new Object() 创建的对象的原型(prototype)未定义?

标签 javascript

Chrome 仅使用以下代码记录 undefined:

<!DOCTYPE html><html lang="en"><body><script>

var cody = new Object();

console.log(cody.prototype);

</script></body></html>

而且我认为它应该是Object.prototype,但现在是undefined,为什么?

我尝试了 for (var p in cody){console.log(p);} 但什么也没得到。

最佳答案

它是具有 prototype 属性的构造函数(在本例中为Object)。一个实际的实例 获取对 the __proto__ property 中原型(prototype)的引用:

var cody = new Object();
console.log(cody.__proto__ === Object.prototype);
// => true

请注意,这是未标准化。来自 MDN 文档:

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

在现代浏览器中,您也可以应该使用Object.getPrototypeOf :

console.log(Object.getPrototypeOf(cody) === Object.prototype);
// => true

关于javascript - 为什么由 new Object() 创建的对象的原型(prototype)未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20334371/

相关文章:

javascript - 如何在 Angular 中运行多个 AND 条件?

javascript - HTML5 Web Workers 在 Firefox 4 中工作,但在 Chrome 12.0.742.122 中不工作

javascript - 如何在大小取决于不同移动屏幕尺寸的 iFrame 中居中对齐内容?

javascript - 如何在 Javascript 中将数组转换为对象

php - 表达式引擎 - PHP - 无法运行函数

Javascript 从网络响应中获取值

javascript - 无法使用 Ajax 将数据列表从 Controller 返回到 View 中的下拉列表。我收到未定义的未定义错误

javascript - Ember.js 自动聚焦于刚刚创建的模板

javascript - 动态循环 json 数据数组时不显示 ID(标题和值)

javascript - 如何在 AngularJS 中创建自动关闭警报消息