简体   繁体   English

JS:Object和Object.constructor之间的区别

[英]JS : Difference between Object and Object.constructor

I don't understand the difference between Object and Object.constructor. 我不了解Object和Object.constructor之间的区别。

Let's take an example : 让我们举个例子:

function Person() {}
var p = new Person();

Person is the function to create objects. 人员是创建对象的功能。 So : 因此:

p instanceof Person //true
p.constructor // Person

Person is the constructor and I can create persons with new Person() Person是构造函数,我可以使用新的Person()创建人员

But Object is also a constructor ( I can create object with new Object()). 但是Object也是一个构造函数(我可以使用new Object()创建对象)。 So why Object has a constructor property since it's already a constructor itself ? 那么,为什么Object具有构造器属性,因为它本身已经是构造器了?

Thanks 谢谢

So why does it have a constructor property since it's already a constructor itself? 那么,为什么既然它本身已经是构造函数,那么为什么要具有构造函数属性呢?

Because every constructor is a function, and those inherit their properties (like the call , apply and bind methods) from Function.prototype . 因为每个构造函数都是一个函数,并且那些构造函数都是从Function.prototype继承其属性(如callapplybind方法)。 Including the Function.prototype.constructor property, which points to Function . 包括指向FunctionFunction.prototype.constructor属性。

constructor is a pointer towards the constructor function used to create a given value, all objects have one. constructor是指向用于创建给定值的constructor function的指针,所有对象都有一个。 For primitives it points towards the primitive constructor: Object , Number , Function and so on. 对于基元,它指向基元构造函数: ObjectNumberFunction等。

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor Doc: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

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

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