Javascript继承静态和实例方法/属性

Javascript继承静态和实例方法/属性,javascript,oop,inheritance,Javascript,Oop,Inheritance,在javascript中,如何设置继承,以便继承静态和实例属性/方法 我的目标是构建一个基础“类”,第三方将从中继承,并且能够调用从基础“类”继承的静态和实例属性/方法 在我的示例中,我定义了一个recipe基类,其他人可以从该基类继承并创建配方: Function.prototype.inherits = function (base) { // code to inherit instance and static // props/methods here var temp =

在javascript中,如何设置继承,以便继承静态实例属性/方法

我的目标是构建一个基础“类”,第三方将从中继承,并且能够调用从基础“类”继承的静态和实例属性/方法

在我的示例中,我定义了一个recipe基类,其他人可以从该基类继承并创建配方:

Function.prototype.inherits = function (base) {
  // code to inherit instance and static
  // props/methods here
  var temp = function () { };
  temp.prototype = base.prototype;
  this.prototype = new temp();
}

function Recipe() {
  var self = this;

  Recipe.ingredients = { };

  Recipe.prepare = function (ingredients) {
    // prepare each of the ingredients...
    var preparedIngredients = ingredients;

    Recipe.ingredients = preparedIngredients;
  };

  self.share = function () {
    console.log('Recipe Shared!');
  };
}

Toast.inherits(Recipe);

function Toast() {
  var self = this;
}

var toast = new Toast();

// Child function should inherit static methods
Toast.Prepare({
  bread: '2 slices',
  butter: '1 knob',
  jam: '1 tablespoon'
});

// Child function should inherit static properties
console.log(Toast.ingredients);

// Child function should inherit instance methods as well
toast.share();

// And if I define another it gets its own static properties/methods
// Spaghetti.ingredients !== Toast.ingredients
Spaghetti.inherits(Recipe);

function Spaghetti() {
  var self = this;
}

Spaghetti.prepare({
  noodles: '1 box',
  tomatoes: 2,
  sausage: 1
});

这里请注意:

你的遗传是错误的,吐司不是一个食谱,它有一个食谱。配方的成分不能是静态的,因为配方可以用于烤面包、煎饼或其他许多菜肴

function Recipe(name,ingredients) {
  this.name=name;
  this.ingredients = ingredients;
};

var Toast = function(){}
Toast.prototype.recipe = new Recipe('toast',{
  bread: '2 slices',
  butter: '1 knob',
  jam: '1 tablespoon'
});
有关prototype的更多信息可在此处找到:

如果要继承静态成员,需要一个更好的示例:例如MyDate和MyDateTime

var MyDate=function(dateString){
  this.date=(dateString)?new Date(dateString)
    :new Date();
};
MyDate.YEAR=Date.prototype.getFullYear;
//... others like MONTH, DAY ...
MyDate.prototype.get = function(what){
  if(what && typeof what==='function'){
      return what.call(this.date);
  }
  console.log('nope');
};

var MyDateTime=function(dateString){
  MyDate.call(this,dateString);
};
//set static properties (can write a function for this)
for(mystatic in MyDate){
  if(MyDate.hasOwnProperty(mystatic)){
    MyDateTime[mystatic]=MyDate[mystatic];
  }
}
MyDateTime.HOUR=Date.prototype.getHours;
//instead of breaking encapsulation for inheritance
// maybe just use Object.create and polyfil if needed
// and stop reading DC when it comes to this subject
MyDateTime.prototype=Object.create(MyDate.prototype);
MyDateTime.prototype.constructor=MyDateTime;

var d = new MyDate();
console.log(d.get(MyDate.YEAR));
var dt = new MyDateTime();
console.log(dt.get(MyDateTime.YEAR));
console.log(dt.get(MyDateTime.HOUR));

下面是一个静态/实例成员继承的示例。这是使用我编写的小型类框架,使javascript中的oop更易于使用,外观更美观。如果你感兴趣的话

var Base = new ds.class({
    type: 'Base',
    constructor: function() {
        this.instanceprop: 'am i instance?'
    },
    staticprop: 'am i static?'
});

var Example = new ds.class({
    type: 'Example',
    inherits: Base,
    constructor: function() {}
});

var eb = new Base();
var ex = new Example();

console.log( Base.staticprop );
console.log( eb.instanceprop );
console.log( Example.staticprop );
console.log( ex.instanceprop );

为什么
Child
方法要将
Base.staticProp
对象上的属性设置为
'Child'
?@Bergi查看我更新的演示。我为其他菜谱定义了一个菜谱基类,如Toast、意大利面条等。可能让您感到困惑的是,我希望每个子类都有自己的静态属性。呃,我认为
Toast
应该是
菜谱
的一个实例?
成分
显然不符合静态属性的条件。@Bergi在本例中,我们只说Toast是所有Toast的基础,因此它需要是一个类(它是一个模型)。其他种类的土司可能会从toast.Hm扩展而来,但它们仍然不会共享
.components
。而且,
Recipe
可能成为生成模型的元类,这有点复杂。