【发布时间】:2015-06-15 20:46:57
【问题描述】:
我想从现有类“扩展”一个新类,并包含它的方法和参数构造。
System = function(name, hp){
this.name = name;
this.hp = [];
this.setHP = function(hp){
this.hp[0] = hp;
this.hp[1] = hp;
}
this.setHP(hp);
}
Weapon = function(name, hp){
System.call(this);
}
Weapon.prototype = new System(name, hp);
Weapon.prototype.constructor = Weapon;
var gun = new Weapon("Gun", 10); // undefined name, hp
var hangar = new System("Hangar", 10); // works
所以,就我所知,有人显然是错的。 有人可以给我建议吗?
【问题讨论】:
-
call需要参数Weapon = function(name, hp){ System.call(this, name, hp); } -
我可以询问哪种解决方案更好用吗?按照建议展开通话或使用 apply ?
-
@Cristian F this 可能会有所帮助
标签: javascript class object