admin管理员组文章数量:1334342
From the javascript console in Chrome:
> function Person(name){this.name=name;}
undefined
At this point, Person.prototype should be an empty Object according to the Javascript specs. Let's assign it:
> p=Person.prototype
> Person
Note that that > Person is clickable and it expands to:
constructor: function Person(name){this.name=name;}
__proto__: Object
But... wasn't it meant to be an empty object? What is all the extra stuff? If you do an alert:
alert(p)
You get [object Object]. Why, when you type it in the Chrome console, does it e out with > Person which expands? Wasn't it meant to be an empty object?
Thank you!
From the javascript console in Chrome:
> function Person(name){this.name=name;}
undefined
At this point, Person.prototype should be an empty Object according to the Javascript specs. Let's assign it:
> p=Person.prototype
> Person
Note that that > Person is clickable and it expands to:
constructor: function Person(name){this.name=name;}
__proto__: Object
But... wasn't it meant to be an empty object? What is all the extra stuff? If you do an alert:
alert(p)
You get [object Object]. Why, when you type it in the Chrome console, does it e out with > Person which expands? Wasn't it meant to be an empty object?
Thank you!
Share Improve this question asked Jul 1, 2011 at 22:54 MercMerc 17.1k18 gold badges84 silver badges132 bronze badges3 Answers
Reset to default 10No, the prototype
always has the constructor
property which points to the function it is the prototype of. And of course it inherits from an object too, that is the internal __proto__
property.
It is defined in ECMAScript 5 Section 13.2, Creating Function Objects:
(...)
16. Let proto be the result of creating a new object as would be constructed by the expression
new Object()
whereObject
is the standard built-in constructor with that name.17. Call the [[DefineOwnProperty]] internal method of proto with arguments
"constructor"
, Property Descriptor {[[Value]]: F, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false.18. Call the [[DefineOwnProperty]] internal method of F with arguments
"prototype"
, Property Descriptor {[[Value]]: proto, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}, and false.(...)
This means nothing else than:
Create a new empty object called proto (16). Define the property constructor
on that object and set the value to F (the function itself) (17). Then define the property prototype
on the function F and set its value to proto.
If you alert
an object, then the object is converted to a string. The default behaviour is to convert an object to the [object Object]
string, unless the "special" toString
method is overridden.
The Chrome console lists these properties because it is meant for debugging, so you need information. [object Object]
is not very informative.
FWIW, an empty object looks like this:
You can also see the internal __proto__
property here. An empty object always inherits some default properties, but it does not have own properties.
Chrome's console is a developer tool. It is meant to show in-depth info. In this case, you're looking at the pre-defined properties of the class you just defined.
Those are methods and properites inherited from the Object class.
It discusses the defaults here
本文标签: Chrome39s Javascript console what does it output in terms of objectsStack Overflow
版权声明:本文标题:Chrome's Javascript console: what does it output in terms of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223220a2435586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论