Object.constructor === Object.constructor.constructor //为什么? [英] Object.constructor===Object.constructor.constructor // why?

查看:107
本文介绍了Object.constructor === Object.constructor.constructor //为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处声明 https://developer.mozilla.org/en/JavaScript/ Reference / Global_Objects / Function 函数对象实例的构造函数属性指定创建对象原型的函数。这很令人困惑,所以 Object.constructor 是创建对象原型的函数?究竟什么对象是对象?

stated here https://developer.mozilla.org/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?

我试图理解为什么Object.constructor的构造函数属性本身?

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

如下:Object.constructor === Object.constructor.constructor //为什么?

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

编辑:我发现TJ克劳德的答案很好,但他的话的措辞非常含糊(在初读时很难理解,至少对我而言)。以下是重新说明的答案:

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)对象功能

2)对象没有名为构造函数的属性所以当我们调用 Object.constructor 时,它实际上给了我们对象。[[原型]]。构造函数(又名对象.__ proto __。构造函数)。

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 (又名对象.__ proto __。构造函数)是函数的一个实例

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

4)由于对象 Object.constructor (又名对象。 __ proto __。constructor )是功能的实例,因此它们都有 __ 原型 __ 引用同一个对象的属性。换句话说, Object .__ proto __ === Object.constructor .__ proto __ (又名 Object .__ proto __。构造函数._ proto _

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)行 Object.constructor = == Object.constructor.constructor 实际上等于对象.__ proto __。constructor === Object.constructor .__ proto __。构造函数

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

6)结合步骤4和5给我们 Object.constructor === Object.constructor.constructor

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

7)转到步骤4)

推荐答案

因为对象函数函数构造函数是 Function ,所以它的构造函数本身。

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

对象是面向对象编程的基本构建块。 JavaScript中的 Object 构造函数(如 Date 正则表达式)。它的工作是通过 new 关键字初始化解释器创建的对象的新实例。

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.

这可能是关闭的-topic,或者不是,因为你问的是构造函数:

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

JavaScript中的任何函数都可以是构造函数;这纯粹是你如何使用它的问题。考虑:

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

function Foo() {
}

如果我这样称呼:

var f = Foo();

...这只是一个无聊的旧功能, f 收到 undefined (因为 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. 解释器创建一个新的空白对象。

  2. 解释器设置新对象的底层原型是 Foo.prototype

  3. 解释器调用 Foo 以某种方式使这个引用新对象。

  4. Foo 完成,如果 Foo 没有返回值(或者不返回对象),则 new expression是在步骤1中创建的对象。(如果 Foo 返回一个对象,则使用该对象;这是大多数人不必做的高级操作。)

  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 completes, 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.)

这篇关于Object.constructor === Object.constructor.constructor //为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆