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

标签 javascript oop inheritance

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

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

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

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

请输入:http://plnkr.co/edit/x8rKeKXSxDHMB4CD6j1c

最佳答案

你的继承是错误的,Toast不是Recipe,它有Recipe。食谱的成分不能是静态的,因为食谱可以用于 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'
});

有关原型(prototype)的更多信息可以在这里找到: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));

关于Javascript继承静态和实例方法/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253831/

相关文章:

c++ - 在派生类中使用私有(private)抽象方法

javascript - 从嵌套的 Javascript 对象中提取和合并数组

javascript - updatepanel 触发后功能不起作用

javascript - 使用 Jquery 了解 Drupal 7 表单中的匿名用户

c# - .net 中的垃圾收集最佳实践

oop - 去OOP实现

perl - 在 Perl 中处理共享公共(public) "ancestor"的模块的多重继承的正确方法是什么?

javascript - 执行innerHTML数字输入的计算

php - 在 PHP 5.2 中,如何在子类中创建一个常量以用于父类中的方法?

c++ - 在 catch 范围内抛出派生类