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
)?
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.
You can't convert the Javascript functions, constructor info etc into JSON using JSON.stringify
, which is why an empty object is returned;
Try this:
console.log('Product prototype: ' , Product.prototype);
console.log('Object prototype: ' , Object.getPrototypeOf(Product.prototype));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.