简体   繁体   English

prototype.constructor和内置Object()有什么区别

[英]What is the difference between prototype.constructor and built-in Object()

A prototype is an object and every function you create automatically gets a prototype property that points to a new blank object. 原型是一个对象,您创建的每个函数都会自动获得一个指向新空白对象的原型属性。 This object is almost identical to an object created with an object literal or Object() constructor, except that its constructor property points to the function you create and not to the built-in Object(). 该对象几乎与使用对象常量或Object()构造函数创建的对象相同,不同之处在于其构造函数属性指向您创建的函数,而不指向内置的Object()。

Above is an excerpt from Stoyan Stefanov's JavaScript patterns book. 上面是Stoyan Stefanov的JavaScript模式书的摘录。

var Ctor = function(msg) { 
    this.msg = msg;
    this.print = function() {
        console.log(this.msg);
    }
};

So, I understand that the Ctor will have a prototype property, which I can get access to. 因此,我知道Ctor将具有一个prototype属性,可以访问该属性。 And to check where the constructor property will point to, I can do: 要检查构造函数属性指向的位置,我可以执行以下操作:

Ctor.prototype.constructor

If I do this on a console, I get the pointer (?) to function Ctor(msg) So far so good. 如果我在控制台上执行此操作,则可以获得function Ctor(msg)的指针(?),到目前为止function Ctor(msg)顺利。 Now I am trying to create a variable through Object() constructor and see where its constructor property points to: 现在,我尝试通过Object()构造函数创建一个变量,并查看其构造函数属性指向的位置:

var CtorCopy = new Object(Ctor);
CtorCopy.prototype.constructor;

Now I see that the result of this on the console is function Ctor(msg) just as the previous case. 现在,我看到在控制台上执行此操作的结果与前面的情况一样是function Ctor(msg) So, which gets me to conclude that the constructor property of the object created through Object() doesn't point to built-in Object() (Actually, I am not sure what built-in Object() means) but to the function I created. 因此,我可以得出这样的结论:通过Object()创建的对象的构造函数属性并不指向内置Object()(实际上,我不确定内置Object()的含义),而是指向该函数。我建立。 This is confusing. 这很混乱。

The text that you quote at the start of your question is referring to the difference between creating an object via a constructor function you've defined and using something like: 您在问题开始处引用的文本是指通过已定义的构造函数创建对象与使用类似方法之间的区别:

var obj = new Object();
// or
var obj = {};

By passing in a value to Object you are invoking a different behaviour which causes it create an object of that Type. 通过将值传递给对象,您正在调用不同的行为,这导致它创建该类型的对象。 According to MDN: 根据MDN:

The Object constructor creates an object wrapper for the given value. Object构造函数为给定值创建一个对象包装器。 If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. 如果该值为null或未定义,它将创建并返回一个空对象,否则,将返回一个类型与给定值相对应的对象。 If the value is an object already, it will return the value. 如果该值已经是一个对象,它将返回该值。

So essentially you've managed to do the same thing just with more code in the same way as my examples above. 因此,从本质上讲,您已经设法以与我上面的示例相同的方式使用更多代码来执行相同的操作。 See here - MDN . 参见此处-MDN

According to MDN (emphasis mine): 根据MDN (重点是我的):

The Object constructor creates an object wrapper for the given value. Object构造函数为给定值创建一个对象包装器。 If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. 如果该值为null或未定义,它将创建并返回一个空对象,否则,将返回一个类型与给定值相对应的对象。 If the value is an object already, it will return the value. 如果该值已经是一个对象,它将返回该值。

Which means that if you pass your Ctor function (which is an object) to the Object constructor, it will simply return the original function back to you. 这意味着,如果将Ctor函数(这是一个对象)传递给Object构造函数,它将仅将原始函数返回给您。 You are looking at two identical objects. 您正在看两个相同的对象。

The above quote in your question is just saying that whenever you create a function that it will have a prototype property attached to it which points to an object, which in it's nature is almost the same as an object created with the build in Object function or literal. 您问题中的上述引述只是说,每当您创建一个函数时,它将附加一个指向对象的原型属性,该属性本质上与使用内置对象函数创建的对象几乎相同,或者文字。

var obj = {};
// or
var obj = new Object();

The difference is just, that its constructor property points to your Ctor function and not to the build in Object function. 所不同的只是,其构造函数属性指向您的Ctor函数,而不是指向内置的Object函数。 You can see the difference if you do the following. 如果执行以下操作,则可以看到区别。

var Ctor = function(msg) { 
  this.msg = msg;
  this.print = function() {
    console.log(this.msg);
  }
};
console.log(typeof Ctor.prototype) // object

So prototype is an object like this one. 因此原型就是这样一个对象。

console.log(typeof {}); // object

But its constructor property will point to your function,... 但是它的构造函数属性将指向您的函数,...

console.log(Ctor.prototype.constructort); // function Ctor()

where as the following will point to the build in Object function. 以下内容将指向内置对象函数。

var obj = {};
console.log(obj.constructor); // function Object()

Since all objects are descended from Object, they all inherit methods and properties form the Object.prototype. 由于所有对象都是从Object派生的,因此它们都继承了Object.prototype的方法和属性。 So if you would want to know which prototype was used to instantiate your Ctor object you could do 因此,如果您想知道用于实例化Ctor对象的原型,可以执行

console.log(Ctor.prototype.__proto__); // Object {}

which will give you the build in object. 这将为您提供内置对象。

Which will explain why the following returns true. 这将解释为什么以下内容返回true。

console.log(Ctor instanceof Object); // true

So if you are using new Object to create an object of your Ctor function, which is nothing but an object and which pretty much is the same as doing this 因此,如果您要使用新对象创建Ctor函数的对象,那么它只不过是一个对象,并且与执行此操作几乎相同

var CtorCopy = {};
CtorCopy = Ctor;

It will eventually have a prototype property with a constructor pointing to your Ctor object. 最终它将具有原型属性,并带有指向您的Ctor对象的构造函数。 I hope this will makes it a little bit more clear on what is meant with the build in object. 我希望这将使内置对象的含义更加清楚。 Since you've mentioned that you are not sure what build in object means in this case. 由于您已经提到过,您不确定在这种情况下内置对象的含义。

暂无
暂无

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

相关问题 什么是prototype.constructor? - What is prototype.constructor for? 为什么JS中的某些函数有一个prototype.constructor prop而其他函数没有? 这些功能有什么区别? - Why some functions in JS have a prototype.constructor prop and others don’t? What is the difference between these functions? 原型对象和构造函数上的成员有什么区别? - What is the difference between members on prototype object and on constructor function? 在重新定义构造函数的原型之后,对象构造函数指向原始构造函数而不是prototype.constructor - Object constructor points to the original constructor not prototype.constructor after redefining prototype of constructor function 省略prototype.constructor更改原型链 - Omitting prototype.constructor changes prototype chain Object.create 将 prototype.constructor 更改为父构造函数,但在子实例化时,子构造函数运行 - Object.create changes prototype.constructor to parent constructor, but upon child instantiation, child constructor runs 构造函数的JavaScript prototype.constructor属性不符合规范? - JavaScript prototype.constructor property for constructors not in spec? javascript中的实际使用prototype.constructor - practical use prototype.constructor in javascript 无法理解 <x> .prototype.constructor? - Unable to understand <x>.prototype.constructor? 函数构造函数和原型构造函数有什么区别? - What's the difference between a function constructor and prototype constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM