简体   繁体   English

使用call()进行继承。 为什么它持续存在?

[英]Inheritance using call(). Why does it persists?

I've been reviewing several examples of inheritance using prototype in javascript. 我一直在审查使用javascript中的原型进行继承的几个示例。

Though I understand the main of it, I still don't fully understand in these examples why after calling call() method its effect persist when we create new instances afterwards. 尽管我了解了它的主要内容,但是在这些示例中,我仍然没有完全理解为什么在调用call()方法之后,当我们随后创建新实例时,其效果仍然存在。

Example code from https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/ 来自https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/的示例代码

var asCircle = function() {
  this.area = function() {
    return Math.PI * this.radius * this.radius;
  };
  this.grow = function() {
    this.radius++;
  };
  this.shrink = function() {
    this.radius--;
  };
  return this;
};

var Circle = function(radius) {
    this.radius = radius;
};
asCircle.call(Circle.prototype);
var circle1 = new Circle(5);
circle1.area(); //78.54

I thought that call() assigned this scope in the same moment it is invoked, and only in that moment. 我以为call()会在被调用的那一瞬间(仅在那一瞬间)分配此作用域。 However, after invoking call() we create instances of Circle (circle1) and circle1 is still 'remembering' to use Circle prototype to use asCircle methods. 但是,在调用call()之后,我们创建了Circle(circle1)的实例,并且circle1仍在“记住”使用Circle原型来使用asCircle方法。

I understand way better the similar approach when call() is called everytime an instance is created. 我会更好地理解每次创建实例时都会调用call()的类似方法。 It'd be like: 就像:

 var Circle = function(radius) {
        this.radius = radius;

        asCircle.call(this);
    };

Am I not understanding well how call() persists after being invoked? 我不是很了解call()在调用后如何持续存在?

Would it make any difference between these two snippets in terms of inheritance?: 就继承而言,这两个代码片段之间有什么区别吗?:

function Animal(name){
   this.name = name;
    this.speak = function(){ 
       console.log("my name is: " + this.name);
    }
};
function Cat(name) {
    this.catproperty = "whatever";
    Animal.call(this, name);
}
Cat.prototype = new Animal();

var cat = new Cat('Joe');
cat.speak();

versus: 与:

function Animal(name){
    this.name = name;
    this.speak = function(){ 
       console.log("my name is: " + this.name);
    }
};

function Cat(name) {
 this.name = name;
 this.catproperty = "whatever";
}

Animal.call(Cat.prototype, );

var cat = new Cat('Joe');
cat.speak();

I thought that call() assigned this scope in the same moment it is invoqued, and only in that moment. 我以为call()会在被调用的那一瞬间(仅在那一瞬间)分配此范围。

It sets the value of this for that function call. 它设置的值, this对于函数调用。

The asCircle function modifies the object that this refers to . asCircle功能修改该对象this指的是

After the function finishes running, the value of this goes away. 该函数完成运行后,值this消失。 The changes to the value don't go away. 值的更改不会消失。

asCircle and Circle, both are function constructors. asCircle和Circle都是函数的构造函数。 Function constructors instantiates new objects and sets the property of the objects as defined inside a constructor, when called with the "new" operator. 当使用“ new”运算符调用时,函数构造函数实例化新对象并按照构造函数内部定义的方式设置对象的属性。

Now, when you wrote the below line of code 现在,当您编写下面的代码行时

asCircle.call(Circle.prototype);

it meant that the "this" inside asCircle will directly point to "proptotype" property of the Circle function, hence modifying it. 这意味着asCircle中的“ this”将直接指向Circle函数的“原型”属性,从而对其进行修改。 Hence, any object being created with the Circle function constructor (circle1 in this case) will have access to the modified prototype property. 因此,使用Circle函数构造函数(在这种情况下为circle1)创建的任何对象都可以访问修改后的prototype属性。

PS: In javascript when an object, function or array is passed to any method it is passed by reference and it can be modified permanently by the method to which it is passed. PS:在javascript中,将对象,函数或数组传递给任何方法时,它都是通过引用传递的,并且可以通过传递给它的方法对其进行永久性修改。 Primitives(string, integers etc) are passed by value. 基元(字符串,整数等)按值传递。

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

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