Javascript继承静态和实例方法/属性
Javascript inherit static and instance methods/properties
在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
});
Plnkr here: http://plnkr.co/edit/x8rKeKXSxDHMB4CD6j1c
你的继承是错误的,Toast不是一个Recipe它有一个Recipe。Recipe的配料不能是静态的,因为Recipe可以用于烤面包、煎饼或许多其他菜肴。
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));
这是一个静态/实例成员继承的例子。这是使用我编写的小类框架,使javascript中的oop更容易使用和更好看。如果你感兴趣,可以在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 );
相关文章:
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象不支持属性或方法“自动完成”
- 区分具有相同名称的属性和方法
- SCRIPT438:对象没有't支持属性或方法'endsWith'在IE10中
- javascript表单字段submit:Object dos'在IE8中不支持此属性或方法
- 从实例方法调用实例方法
- 属性比方法快?需要理由
- 对象没有't支持属性或方法'transformNode'在Internet Explorer 1
- jQuery Deferred and promise-错误:对象没有't支持属性或方法'然后'
- 获取“;无法读取属性'[方法]'“未定义”;
- 如何修复错误'对象没有't支持属性或方法'getScreenCTM''在Inter
- JSHint 错误“对象不支持此属性或方法”用于 javascript 闭包
- 对象不支持属性或方法“确认”
- 对象不支持属性或方法“jscomp_symbol_iterator0”
- 对象不支持 igGrid 中的属性或方法“_super”
- Javascript继承静态和实例方法/属性
- 在实例方法中获取对实例属性的访问权
- 用实例方法的结果填充集合中每个项目的属性
- 使用可选属性实例化类的正确方法
- 当将实例方法传递给另一个函数时,如何访问实例方法中的实例属性