简体   繁体   English

将原型链导航到Object.prototype

[英]Navigating prototype chain to Object.prototype

I was trying to figure out how a constructor function's prototype object looks like. 我试图弄清楚构造函数的原型对象的样子。 Thus I tried the below. 因此,我尝试了以下内容。

Any idea why both lines show an empty object? 知道为什么这两行都显示一个空对象吗? Should not the bottom line print the Object 's prototype object ( Object.prototype )? 底线是否应该打印Object的原型对象( Object.prototype )?

function Product(name, price) {
    this.name = name;
    this.price = price;
}
console.log('Product prototype: ' + JSON.stringify(Product.prototype, null, 4));
console.log('Object prototype: ' + JSON.stringify(Object.getPrototypeOf(Product.prototype), null, 4));

Thank you. 谢谢。

JSON.stringify trys to convert javascript objects into JSON format. JSON.stringify尝试将JavaScript对象转换为JSON格式。

You can't convert the Javascript functions, constructor info etc into JSON using JSON.stringify , which is why an empty object is returned; 您无法使用JSON.stringify将Javascript函数,构造函数信息等转换为JSON,这就是为什么返回空对象的原因;

Try this: 尝试这个:

console.log('Product prototype: ' , Product.prototype);
console.log('Object prototype: ' , Object.getPrototypeOf(Product.prototype));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM