了解构造函数调用模式中的

了解构造函数调用模式中的

本文介绍了了解构造函数调用模式中的"this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图理解 this ,并在 javascript精采部分书中找到了一个示例:

Trying to understand this, found an example in javascript the good parts book:

var first;
var second;

var Quo = function(string) {
    first = this;
    this.status = string;// first this
};

Quo.prototype.get_status = function() {
    second = this;
    return this.status;//second this
};

var myQuo = new Quo( "confused" );
console.log( myQuo.get_status() );

console.log( (first===second) + ',' + (second===myQuo) + ',' + (first===myQuo) );

输出:

$ node test.js
confused
true,true,true

第一个 this 和第二个 this 都指向 myQuo 吗?如何打印出每个 this 所指向的对象名称或函数名称或类名称?(目前确实对此 this 感到困惑.)

Does the first this and the second this both point to myQuo? How to print out the object name or function name or class name which each this point to? (Really confused by this currently.)

更新

另一个问题:这两个 this 都引用了Quo的实例而不是Quo的原型?

Another questions: Both this refer to the instance of Quo instead of Quo's prototype?

还尝试:

console.log( myQuo.get_status() );
console.log(first.constructor.name);
console.log( first );
console.log( second );

输出:

confused

{ status: 'confused' }
{ status: 'confused' }

为什么 first.constructor.name 什么都不是?为什么 first {status:'confused'} ?

Why first.constructor.name is nothing? Why first is { status: 'confused' }?

推荐答案

在javascript中,当在实例上调用在原型上定义的函数时;函数内部的"this"表示(想像被替换为-)该实例(或在其原型链中具有原型的拥有对象).

In javascript, when a function defined on a prototype is called on an instance; "this" inside the function means (think like it is replaced with-) that instance (or the owning object having the prototype in its prototype chain).

在您的情况下,构造函数(也是原型上的一个函数)被一个新创建的对象(其关键字为"new")调用为"this".

In your case the constructor (which is also a function on the prototype) is called with a newly created object as "this" with the "new" keyword.

var Quo = function(string) {
    first = this; //this is the newly created instance
    this.status = string;// first this
};

然后还首先分配完全相同的实例.

Then first is also assigned the exact same instance.

在同一实例上调用get_status时,它将再次替换为该实例;

When get_status is called on the same instance, this is again replaced with the instance;

Quo.prototype.get_status = function() {
    second = this; //this is myQuo
    return this.status;//second this
};

这一次分配了相同的实例.

and this time second is assigned the same instance.

执行myQuo.get_status()与;

doing myQuo.get_status() is same as;

Quo.prototype.get_status.call(myQuo)

因此,这两个"this"关键字引用同一对象.

Therefore these two "this" keywords refer to the same object.

如果两个变量指向同一个实例,则进行"==="检查可以很好地进行比较.获得构造函数名称只是告诉您它们是使用特定的构造函数名称构造的,而不是其他.可以使两个不同的对象具有相同的构造函数名称.

Doing the "===" check is perfectly fine to compare if two variables point to the same instance. Getting the constructor name just tell you they were constructed with a specific constructor name, not else. It is possible to make two different objects with the same constructor name.

这篇关于了解构造函数调用模式中的"this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:46