如何调用父类&x27;方法从JavaScript中的子类中删除,以便父';s的局部变量是否可以访问?

如何调用父类&x27;方法从JavaScript中的子类中删除,以便父';s的局部变量是否可以访问?,javascript,oop,inheritance,superclass,Javascript,Oop,Inheritance,Superclass,我正在使用JavaScript中的类继承方法之一(正如我正在修改的代码中所使用的),但不理解如何将子类中的方法的附加功能附加到相应父类方法已经具有的功能上;换句话说,我想用一个方法覆盖子类中父类的方法,这个方法除了它自己的子类特定的东西之外,还与父类的方法做的相同。所以,我试图从子方法调用父方法,但这可能吗 代码在这里:。请打开开发控制台查看输出 代码也在这里: function MakeAsSubclass (parent, child) { child.prototype = new

我正在使用JavaScript中的类继承方法之一(正如我正在修改的代码中所使用的),但不理解如何将子类中的方法的附加功能附加到相应父类方法已经具有的功能上;换句话说,我想用一个方法覆盖子类中父类的方法,这个方法除了它自己的子类特定的东西之外,还与父类的方法做的相同。所以,我试图从子方法调用父方法,但这可能吗

代码在这里:。请打开开发控制台查看输出

代码也在这里:

function MakeAsSubclass (parent, child)
{
    child.prototype = new parent;   // No constructor arguments possible at this point.
    child.prototype.baseClass = parent.prototype.constructor;
    child.prototype.constructor = child;

    child.prototype.parent = child.prototype; // For the 2nd way of calling MethodB.
}

function Parent (inVar)
{
    var parentVar = inVar;

    this.MethodA = function () {console.log("Parent's MethodA sees parent's local variable:", parentVar);};
    this.MethodB = function () {console.log("Parent's MethodB doesn't see parent's local variable:", parentVar);};
}

function Child (inVar)
{
    Child.prototype.baseClass.apply(this, arguments);

    this.MethodB = function ()
    {
        console.log("Child's method start");
        Child.prototype.MethodB.apply(this, arguments); // 1st way
        this.parent.MethodB.apply(this, arguments); // 2 2nd way
        console.log("Child's method end");
    };
}

MakeAsSubclass(Parent, Child);

var child = new Child(7);
child.MethodA();
child.MethodB();

不,您无法看到父局部变量。您继承的是父对象原型链,而不是其本地状态。在您的例子中,您正在将父函数应用到不包含状态的子对象上

apply(this,...)

意味着您正在将函数绑定到此函数的当前值。从子对象调用方法b时,它会绑定到子对象,因此不会在包含父对象值的闭包中操作

变量
var parentVar=inVar
是一个局部和私有变量,仅在
Parent()
函数中可用,并且仅对同一作用域中定义的函数可见(
Parent()

我认为最好的方法是修改父类,使
parentVar
不是私有的,而是公共的:

function Parent(inVar) {

    this.parentVar = inVar;

}

我建议agains使用这样的私有实例值属性,因为这样会弄乱原型。需要访问私有实例变量(每个实例都有自己的私有值)的函数不能放在原型上,因此不能真正使用它

下面是代码的工作方式(但我自己不会这么做):

以下是如何使用“私有”函数:

var Parent = function(name){
  //public instance variable
  this.name=name;
}
Parent.prototype=function(){
  // privates on the prototype (shared among instances)
  var privateFunction=function(me){
    console.log("private function called in:"+me.name);
    console.log("shared in "+me.name
      +" is now:"+shared+" setting it to 'changed'");
    shared="Changed";
  }
  // private shared value
  var shared="Parent"
  return{
    constructor:Parent,
    publicFn:function(){
     privateFunction(this); 
    }
  };
}();

var Child=function(name){Parent.call(this,name)};
Child.prototype=Object.create(Parent.prototype);
Child.prototype.constructor=Child;

var c = new Child("child1");
var c2 = new Child("child2");
c.publicFn();
c2.publicFn();//shared is already changed by 'child1'

有关构造函数的详细信息

否您无法看到父局部变量。您继承了父原型链,而不是它们的本地状态。但是父对象的MethodA(从子对象调用)确实看到了变量。我猜这是因为它是在构造函数中为子对象重新创建的。但是,MethodB没有。您正在对父对象调用。您正在对子对象调用B。如果你用apply(这个)调用A,它也会失败好吧,我觉得很奇怪。:)我的意思是,我确实
child.MethodA(),但事实证明我实际上是在父对象上调用MethodA!感谢您参考私有函数和私有静态/共享变量!但是关于私有实例值:您的示例与我的示例到底有什么不同?“showPrivate”函数看起来很像我的“MethodA”,它可以很好地访问局部变量。“MethodB”是我遇到的问题。@Janis我不知道,不知道你的代码应该做什么。根据对您问题的描述,我试图发布代码,说明如何重写父函数。使用私有实例值属性(构造函数函数体中的所有内容)的示例我猜不会显示为重写,因为我从未使用过它。我想你可以保存父函数,然后在调用父函数之前调用它。在子函数体内调用(这个)。
var Parent = function(name){
  //public instance variable
  this.name=name;
}
Parent.prototype=function(){
  // privates on the prototype (shared among instances)
  var privateFunction=function(me){
    console.log("private function called in:"+me.name);
    console.log("shared in "+me.name
      +" is now:"+shared+" setting it to 'changed'");
    shared="Changed";
  }
  // private shared value
  var shared="Parent"
  return{
    constructor:Parent,
    publicFn:function(){
     privateFunction(this); 
    }
  };
}();

var Child=function(name){Parent.call(this,name)};
Child.prototype=Object.create(Parent.prototype);
Child.prototype.constructor=Child;

var c = new Child("child1");
var c2 = new Child("child2");
c.publicFn();
c2.publicFn();//shared is already changed by 'child1'