在JavaScript类中调用父方法,但stll可以访问对象实例内部的原型方法吗? [英] Call parent method in JavaScript class but stll have access to prototype methods inside object instance?

查看:91
本文介绍了在JavaScript类中调用父方法,但stll可以访问对象实例内部的原型方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JavaScript类中调用父方法,但仍然可以从父子类访问原型方法.这是代码示例:

Is it possible to call parent method in JavaScript class but to still have access to prototype methods from parent and child class. Here is code example:

var Base = function() {

  this.baseMethod = function(){
    return 'baseMethod';
  };

  this.baseInitMethod = function() {
    return 'baseInitMethod';
  }
}


Base.prototype.basePrototypeMethod = function() {
  return "basePrototypeMethod";
};


var Specific = function() {

  Base.call(this);

  this.baseInitMethod = function() {
    // call baseInitMethod from Base class
  }

  this.specificMethod = function(){
    return 'specificMethod';
  }

  this.specificInitMethod = function() {

    return this.basePrototypeMethod();
  }
}


Specific.prototype.specificPrototypeMethod = function() {
  return 'specificPrototypeMethod' + '-' + this.baseInitMethod();
}


for(var p in Base.prototype) {
   Specific.prototype[p] = Base.prototype[p]
}


var s = new Specific();


console.log(s.baseMethod());

console.log(s.baseInitMethod());

console.log(s.basePrototypeMethod());

console.log(s.specificMethod());

console.log(s.specificInitMethod());

console.log(s.specificPrototypeMethod());

我想从特定类中的baseInitMethod方法中调用Base类中的baseInitMethod,但是从上面进行的所有函数调用仍然有效.有可能吗?

I want to call baseInitMethod in Base class from baseInitMethod method inside Specific class but so that all function calls from above still works. Is that possible?

推荐答案

您的Specific.prototype对象应该从Base.prototype对象继承.当前,您正在使用以下代码将其所有属性复制到对象:

Your Specific.prototype object should inherit from the Base.prototype object. Currently you're copying over all its properties to the object with this code:

for(var p in Base.prototype) {
   Specific.prototype[p] = Base.prototype[p]
}

但是您实际上应该使用 Object.create 建立真实的原型链:

But you should actually use Object.create to establish a real prototype chain:

Specific.prototype = Object.create(Base.prototype);

Specific.prototype.specificPrototypeMethod = function() {
  return 'specificPrototypeMethod' + '-' + this.baseInitMethod();
}


我想从特定类中的baseInitMethod方法中调用Base类中的baseInitMethod

I want to call baseInitMethod in Base class from baseInitMethod method inside Specific class

是的.在您的Specific构造函数中,首先需要获取BasebaseInitMethod实例方法,然后再覆盖实例的属性:

Yes. In your Specific constructor, you first need get Base's baseInitMethod instance method, before you overwrite the property of the instance:

function Specific() {
    Base.call(this);

    var parentInitMethod = this.baseInitMethod;
    this.baseInitMethod = function() {
        // call baseInitMethod from Base class:
        parentInitMethod.call(this /*, arguments…*/); 
    }

    …
}

以便从上面进行的所有函数调用仍然有效.

so that all function calls from above still works.

我不确定您的意思是什么. specificPrototypeMethod将始终调用当前实例的baseInitMethod,这将被Specific覆盖,而不是Base中定义的原始实例.

I'm not sure what you mean by that exactly. The specificPrototypeMethod will always call the baseInitMethod of the current instance, which would be Specific's overwritten one not the original that was defined in Base.

这篇关于在JavaScript类中调用父方法,但stll可以访问对象实例内部的原型方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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