为什么我们要使用“ Boy.prototype = new Human;”来模拟继承? [英] why do we use `Boy.prototype = new Human;` to simulate inheritance?

查看:93
本文介绍了为什么我们要使用“ Boy.prototype = new Human;”来模拟继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么每个人都使用 Boy.prototype = new Human; 来模拟继承。看,我们想要的是A的功能吗?我们可以在不实例化新A的情况下做到这一点(实际上,实例化新A确实会给我们带来不良的结果,就某种意义上说,我们实际上正在运行实例化函数,而这并不是我们想要的)



那么这不是更好的解决方案吗?

  for(Human.prototype中的var prop_name){
Object.defineProperty(
Boy.prototype,
prop_name,
Object.getOwnPropertyDescriptor(Human.prototype,prop_name)
);
}

说我们很特别,不仅希望Human.prototype中具有可枚举的属性我们仍然可以通过使用 Object.getOwnPropertyNames 并在原型链上调用它来实现,而原型链又可以通过 Object.getPrototypeOf



那么 Boy.prototype = new Human; 做的确切好处是什么?当我们有更好的选择可用时,可以模拟继承吗?

解决方案

更好的选择是创建一个用于保存原型的中间体。 / p>

 函数extend(clazz,超类){
var middle = function(){};
Intermediate.prototype = superclass.prototype;
clazz.prototype =新的中间体();
//下一行是可选的,但很有用
clazz.prototype.constructor = clazz;
}

这可以避免不必要的复制,但仍然意味着您无需实例化一个将在其构造函数中起作用的对象。它还设置了原型链,以便您可以使用instanceof。



为了完整起见,您的子类应在其构造函数中调用超类构造函数,您可以使用它来处理超类原型污染。 Superclass.call(this);



编辑:自ES5起,您可以用

  Subclass.prototype = Object.create(将对 extend 的调用替换为

  Superclass.prototype); 

这具有相同的作用。


i don't get why everyone is using Boy.prototype = new Human; to simulate inheritance. Look, what we want is the function's of A right? we can do that without instantiating a new A (in fact instantiating a new A does give us undesirable results, in the sense that we are actually running the instantiating function which isn't what we want)

So isn't this a better solution?

for (var prop_name in Human.prototype) {
Object.defineProperty(
          Boy.prototype,
          prop_name,
          Object.getOwnPropertyDescriptor(Human.prototype,prop_name)
        );
}

Say we are that particular and want not only the enumerable properties in Human.prototype we could still achieve it by using Object.getOwnPropertyNames and calling it on the prototype chain, which in turn is available to us through Object.getPrototypeOf.

So what exactly is the benefit of doing Boy.prototype = new Human; to simulate inheritance when we have better options available to us?

解决方案

A better option is to create an intermediate to hold the prototype.

function extend(clazz, superclass) {
    var intermediate = function() {};
    intermediate.prototype = superclass.prototype;
    clazz.prototype = new intermediate();
    // Following line is optional, but useful
    clazz.prototype.constructor = clazz;
}

This avoids unnecessary copying, but still means that you don't need to instantiate an object that will do work in its constructor. It also sets up the prototype chain so that you can use instanceof. It also doesn't result in superclass prototype contamination which some inheritance antipatterns can.

For completeness, your subclass should call the superclass constructor in its constructor, which you can do with Superclass.call(this);.

EDIT: Since ES5, you can replace calls to extend with

Subclass.prototype = Object.create(Superclass.prototype);

which does the same thing.

这篇关于为什么我们要使用“ Boy.prototype = new Human;”来模拟继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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