【问题标题】:How to implement partial inheritance in javascript?如何在javascript中实现部分继承?
【发布时间】:2017-12-07 06:44:42
【问题描述】:

我昨天参加了一次面试,面试官问我关于部分继承的问题,我对此一无所知。根据我的理解,我在下面提到了 2 个代码块。让我知道我所做的一切对于部分继承是否正确。

代码块 1:我们可以称之为部分继承吗?

var Person = function(){}

Person.prototype.getName = function(){
    return this.name;
}

Person.prototype.setName = function(name){
    this.name = name;
}

var Author = function(name, books){
    this.books = books;
    Person.prototype.setName.call(this, name); <<---- Is this a way of doing partial inheritance?
}

Author.prototype.getBooks = function(){
    return this.books;
}

var author = new Author('Auth1', ['b1','b2']);

代码块 2:如何仅从“Person”构造函数继承“setName”函数?有可能吗?

var Person = function(){
    this.setName = function(name){
        this.name = name;
    };
}

Person.prototype.getName = function(){
    return this.name;
}

var Author = function(name, books){
    this.books = books;
}

Author.prototype.getBooks = function(){
    return this.books;
}

var author = new Author('Auth1', ['b1','b2']);

请解释以上2个代码块的真实性吗?

我什至不知道上面引用的例子是否正确。如果它们不正确,请说明 JavaScript 中的部分继承。

问题:我只想从 Person 构造函数继承 setName 方法到 Author 构造函数。这可能吗?

【问题讨论】:

  • 你可以在这里找到非常清晰的解释:w3schools.com/js/js_object_prototypes.asp
  • 这里没有谈到部分继承。它存在还是不存在?如果不是,那么面试官可以在什么情况下提出这个问题?
  • 当你说“继承”函数时,你真正的意思是得到一个使用this(未绑定)的函数。如果你 bind 某事,this 将被实际实例对象替换,并且不再是可以在任何上下文中调用的泛型类方法。 callapply 之间的区别在于applyargumentsarrays 一起使用(注意它们都以'a' 开头)
  • 我以前从未听说过“部分继承”这个词。
  • 为什么setName 是在Person 构造函数中创建的,而不是作为Person.prototype 的常规方法?它甚至不使用构造函数范围内的任何东西。

标签: javascript oop inheritance


【解决方案1】:

通过阅读您的描述,我认为您并不熟悉上下文和this 的概念,this 是可以代表任何对象的通配符。另外值得注意的是,原型链只是查看某个属性是否在类实例上未定义的特定位置。实例只是运行构造函数后返回的常规对象(通过“new”关键字),其中函数 class.prototype.asdf 与 instance.asdf 相同,如果未定义 instance.asdf

const OtherClass = function () {};
const MyClass = function () {};
      MyClass.prototype.logThis = function () { console.log(this); };

const otherInstance = new OtherClass();
const myInstance = new MyClass();

myInstance.logThis(); // logs myInstance
myInstance.logThis.call(otherInstance, 'param1', 'param2'); // logs otherInstance
myInstance.logThis.apply(otherInstance, ['param1', 'param2']); // logs otherInstance

记住“this”是什么的简单方法——在执行或调用时“点左侧”的任何内容,而不是编写它的函数范围。这就是为什么 setTimeout 和其他异步事物(如 Promises)不会使用您可能期望的“this”,除非您已经绑定它。

您可以编写一个通用的、共享的函数,对未知数进行操作,并将其包含在不同的对象中。

Person.prototype.setName = Author.prototype.setName = function setName () {}

【讨论】:

  • 我只想从 Person 构造函数继承 setName 方法到 Author 构造函数。这可能吗?
  • @AnkurGupta 构造函数是类上的一个特殊方法,我想你的意思是放到原型链上,这样所有的 Author 实例都引用同一个函数。你可以写Author.prototype.setName = Person.prototype.setName 就可以了
  • 但是当我在其他构造函数中使用相同的方法时,为什么要重复相同的方法。它将引入代码冗余。另外我不想创建任何其他构造函数。问题是如何用上面的例子实现部分继承(只有2个构造函数)。
  • 如果你使用类继承,你不应该通过构造函数在每个实例中定义一个共享方法,你应该有Person.prototype.setName = function () {}
  • Person.prototype.setName = Author.prototype.setName = function setName () {}
猜你喜欢
  • 2013-07-25
  • 1970-01-01
  • 2017-05-06
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
相关资源
最近更新 更多