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

时间:2014-08-11 23:10:57

标签: javascript oop inheritance

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

我的目标是构建第三方可以继承的基类“类”,并且能够调用从基类“class”继承的静态和实例属性/方法。

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

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
});

Plnkr:http://plnkr.co/edit/x8rKeKXSxDHMB4CD6j1c

2 个答案:

答案 0 :(得分:1)

你的继承是错误的,Toast不是食谱它有食谱。食谱的成分不能是静态的,因为食谱可以用于烤面包,煎饼或许多其他菜肴。

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'
});

有关原型的更多信息,请访问:https://stackoverflow.com/a/16063711/1641941

如果你想继承静态成员,你需要一个更好的例子:例如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));

答案 1 :(得分:0)

这是静态/实例成员继承的示例。这是使用我编写的小类框架,使jop中的oop更容易使用,更好看。如果您有兴趣,那就是available on github

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 );