最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object.constructor===Object.constructor.constructorwhy? - Stack Overflow

programmeradmin6浏览0评论

stated here the constructor property of an instance of a function object "specifies the function that creates an object's prototype". This is confusing, so Object.constructor is "the function that creates an object's prototype"? What object is "an object" exactly?

I'm trying to understand why is Object.constructor's constructor property itself?

as such: Object.constructor===Object.constructor.constructor // why?

Edit: i find T.J. Crowder's answer good but the phrasing of his words is pretty vague (making it hard to understand at first read, at least for me). Here's the rephrased answer:

1) Object is an instance of Function

2) Object does not have a property called constructor so when we call Object.constructor, it actually gives us Object.[[prototype]].constructor (aka Object.__proto__.constructor).

3) Object.constructor (aka Object.__proto__.constructor) is an instance of Function.

4) Since both Object and Object.constructor (aka Object.__proto__.constructor) are instances of Function therefore they both have a __proto__ property which refer to the same object. In other words Object.__proto__ === Object.constructor.__proto__ (aka Object.__proto__.constructor._proto_)

5) The line Object.constructor===Object.constructor.constructor is actually equal to the line Object.__proto__.constructor===Object.constructor.__proto__.constructor

6) bining steps 4 and 5 give us Object.constructor===Object.constructor.constructor

7) goto step 4)

stated here https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Function the constructor property of an instance of a function object "specifies the function that creates an object's prototype". This is confusing, so Object.constructor is "the function that creates an object's prototype"? What object is "an object" exactly?

I'm trying to understand why is Object.constructor's constructor property itself?

as such: Object.constructor===Object.constructor.constructor // why?

Edit: i find T.J. Crowder's answer good but the phrasing of his words is pretty vague (making it hard to understand at first read, at least for me). Here's the rephrased answer:

1) Object is an instance of Function

2) Object does not have a property called constructor so when we call Object.constructor, it actually gives us Object.[[prototype]].constructor (aka Object.__proto__.constructor).

3) Object.constructor (aka Object.__proto__.constructor) is an instance of Function.

4) Since both Object and Object.constructor (aka Object.__proto__.constructor) are instances of Function therefore they both have a __proto__ property which refer to the same object. In other words Object.__proto__ === Object.constructor.__proto__ (aka Object.__proto__.constructor._proto_)

5) The line Object.constructor===Object.constructor.constructor is actually equal to the line Object.__proto__.constructor===Object.constructor.__proto__.constructor

6) bining steps 4 and 5 give us Object.constructor===Object.constructor.constructor

7) goto step 4)

Share Improve this question edited May 12, 2011 at 3:14 Pacerier asked May 11, 2011 at 11:34 PacerierPacerier 89.8k111 gold badges384 silver badges644 bronze badges 2
  • joost.zeekat.nl/constructors-considered-mildly-confusing.html – Ateş Göral Commented May 12, 2011 at 3:02
  • A nice diagram of JS object layout is here.mollypages/tutorials/js.mp – Jyoti Prasad Pal Commented Aug 20, 2017 at 16:30
Add a ment  | 

2 Answers 2

Reset to default 5

Because Object is a Function, and the Function constructor is a Function, so its constructor is itself.

An "object" is the fundamental building block of object-oriented programming. Object, in JavaScript, is a constructor function (like Date, or RegExp). Its job is to initialize new instances of objects created by the interpreter via the new keyword.

This may be off-topic, or not, since you're asking about constructors:

Any function in JavaScript can be a constructor function; it's purely a matter of how you use it. Consider:

function Foo() {
}

If I call that like this:

var f = Foo();

...it's just a boring old function, and f receives undefined (since Foo doesn't return anything). But if I call it like this:

var f = new Foo();

...I'm using it as a constructor function and something more interesting happens:

  1. The interpreter creates a new, blank object.
  2. The interpreter sets the new object's underlying prototype to be Foo.prototype.
  3. The interpreter calls Foo in a way that makes this refer to the new object.
  4. When Foo pletes, if Foo doesn't return a value (or doesn't return an object), the result of the new expression is the object created in step 1. (If Foo returns an object, that object is used instead; that's an advanced thing most people don't have to do.)

JavaScript Object Layout

发布评论

评论列表(0)

  1. 暂无评论