Javascript 继承静态和实例方法/属性 [英] Javascript inherit static and instance methods/properties

查看:72
本文介绍了Javascript 继承静态和实例方法/属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In javascript how would I set up inheritance so that static and instance properties/methods are inherited?

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

My goal is to build a base "class" that 3rd parties would inherit from, and be able to call static and instance properties/methods inherited from the base "class".

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

In my example I define a recipe base class that others can inherit from and create recipes off of:

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

推荐答案

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

Your inheritance is wrong, Toast is not a Recipe it has a Recipe. Ingredients of Recipe cannot be static because a Recipe can be used for toast, pancake or many other dishes.

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

More info on prototype can be found here: https://stackoverflow.com/a/16063711/1641941

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

If you want to inherit static members you need a better example: for example MyDate and 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 继承静态和实例方法/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆