当人们容易使用apply()和call()方法继承时,为什么人们在javascript中使用原型? [英] Why people use prototype in javascript when it is easy to inherit using apply () and call () methods?

查看:98
本文介绍了当人们容易使用apply()和call()方法继承时,为什么人们在javascript中使用原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

形状是由矩形继承的.这种继承可以通过许多方法来完成.在这里,我使用了apply()和call().当draw方法为child时,从该方法再次调用基类的draw方法.我已经以两种方式完成了这件事.一种方法是制作基类的原型绘制方法,另一种方法是使用apply()和call()方法.
第一种方法:

function Shape () {
  this.name='Shape';
  this.getName = function () {
   return this.name;
  };
  this.draw = function () {
   alert("Something");
  };
} 

function Rectangle () {
  Shape.apply(this);
  var X=this.draw;
  this.name = 'rectangle';
  this.id=500;
  this.draw = function () {
    X.call(this);
  };
}

第二种方法:

function Shape () {
  this.name='Shape';
  this.id=100;
  this.getName = function () {
    return this.name;
  };
}

Shape.prototype.draw = function() {
  alert("Something");
};

function Rectangle () {
  this.name = 'rectangle';
  this.id=200;  
  this.draw = function () {
    Shape.prototype.draw.call(this);
  };
}

Rectangle.prototype = new Shape();
Rectangle.prototype.constructor = Rectangle;

这两种方法都做类似的事情(在提供输出的情况下).我知道通过使用apply()和call()方法无法直接访问基类的原型.对我来说,使用apply()和call()进行继承似乎不太复杂.如果两者相同,那么人们为什么不那么多使用apply()和call()?为什么需要使用原型?如果我不使用原型并使用apply()和call()继承基类,我将面临什么问题?

解决方案

继承使您能够使用基类的方法(和属性),而不必在基础类中显式创建(或链接到它们).派生类.

您的替代"方法将要求执行的每个方法通过每个派生类进行代理,即使该派生类没有专门使用该方法. /p>

使用原型可以避免这种情况,因为每次调用方法或访问派生类中不存在的属性时,JS解释器都会自动遍历属性链,直到它在超类中找到该方法为止.

Shape is inherited by rectangle. This inheritance can be done by many methods. Here I have used apply() and call (). When draw method is child is called, from that method draw method of base class is called again. I have done this thing in two ways. One is making the prototype draw method of base class and the other one is by using apply() and call() method.
First Method :

function Shape () {
  this.name='Shape';
  this.getName = function () {
   return this.name;
  };
  this.draw = function () {
   alert("Something");
  };
} 

function Rectangle () {
  Shape.apply(this);
  var X=this.draw;
  this.name = 'rectangle';
  this.id=500;
  this.draw = function () {
    X.call(this);
  };
}

Second Method :

function Shape () {
  this.name='Shape';
  this.id=100;
  this.getName = function () {
    return this.name;
  };
}

Shape.prototype.draw = function() {
  alert("Something");
};

function Rectangle () {
  this.name = 'rectangle';
  this.id=200;  
  this.draw = function () {
    Shape.prototype.draw.call(this);
  };
}

Rectangle.prototype = new Shape();
Rectangle.prototype.constructor = Rectangle;

Both of these two methods does the similar thing (in case of providing output). I know that by using apply () and call () method I can't directly get the access of base class's prototypes. Inheritance using apply() and call() seems less complicated to me. If both are same then why don't people use apply() and call() that much ? Why do I need to use prototypes ? What problem will I face if I don't use prototypes and inherit base classes using apply() and call () ??

解决方案

Inheritance gives you the ability to use methods (and properties) of the base class without having to explicitly create them (or chain to them) in the derived class.

Your "alternate" method would require that every method that Shape implements be proxyed via every derived class even if that derived class does not specialise that method.

Using the prototype avoids this, because any time you call a method or access a property that doesn't exist in the derived class, the JS interpreter automatically traverses the property chain until it finds that method in a super class.

这篇关于当人们容易使用apply()和call()方法继承时,为什么人们在javascript中使用原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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