Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有父变量的javascript继承_Javascript_Class_Inheritance_Prototype - Fatal编程技术网

带有父变量的javascript继承

带有父变量的javascript继承,javascript,class,inheritance,prototype,Javascript,Class,Inheritance,Prototype,我希望用户能够通过给定对象文本的一个函数,从基类和其他用户创建的类创建类 因此会有一个类别列表: var allclasses = {}; 父类可以是这样的,所有类都有这个祖先: 基本=功能{ this.name='base'; this.varname=250; } base.prototype={ foo:函数{ this.varname+=50; } } //添加到所有类 所有类['base']=base 以及创建新类的方法: function create(childliteral)

我希望用户能够通过给定对象文本的一个函数,从基类和其他用户创建的类创建类

因此会有一个类别列表:

var allclasses = {};
父类可以是这样的,所有类都有这个祖先: 基本=功能{ this.name='base'; this.varname=250; } base.prototype={ foo:函数{ this.varname+=50; } } //添加到所有类 所有类['base']=base

以及创建新类的方法:

function create(childliteral) { ???? }
理想情况下,这将如何工作的示例:

var myclass = create({
   parentname: 'base', // a required variable, create will return false if this isn't valid (not in allclasses)
   name: 'specialname', // a required variable, create will return false if this is already in allclasses
   foo: function() {
     this.varname += 100;
     // this is the part is what causes the most stress, I want it to call the parents function 'foo'
     this.parent.foo();
     return this.varname;
   }
});
然后创建该类的实例:

var myobject = new myclass();
或者

最后,打电话

myobject.foo();
console.log(myobject.varname) // prints out 400

myobject.parent.foo();
console.log(myobject.varname) // prints out 450
console.log(myobject.parent.varname) // also prints out 450
console.log(myobject.parent.name) // prints out 'base'
console.log(myobject.parentname) // prints out 'base'
console.log(myobject.name) // prints out 'specialname'

我已经非常接近这一点,只是我不能链接两个以上的对象。

您需要以某种方式存储对超级类的引用;比如说

 var myclass = create({
    parentName: 'Base',
    name: 'specialname', 
    childCtor: function () { },
    childProto: {
      foo: function(){
         this.varname += 100;
         this._base_.foo(); // reference to the superclass
         return this.varname;
      }
    }
});

请看完整示例

向我们展示您当前的代码!请注意,此.parent…将丢失当前实例的上下文,因此您可能需要重新考虑该模式。另外,你也可以看一下T.J.Crowder的作品。但是在这里使用class这个词要谨慎;它常常带来其他语言的期望,而Javascript基于原型的继承很难满足这些期望,同时也带来了JS中不存在的语言的限制。此外,许多JS人对此很恼火-您需要更好地解释要传递到create中的选项method@scottsauyet谢谢你的链接!它又好又轻。这就是我决定去的。
 var myclass = create({
    parentName: 'Base',
    name: 'specialname', 
    childCtor: function () { },
    childProto: {
      foo: function(){
         this.varname += 100;
         this._base_.foo(); // reference to the superclass
         return this.varname;
      }
    }
});