我最近一直在研究Javascript原型,可以理解该理论,并认为它对我对语言的理解非常有用,但不能完全满足以下要求……
var player = function(){//Declarations here};
var super_player.prototype = new player();
每个编译器/检查器都在第2行中标记“缺少分号”错误。
我很沮丧,但是相信我忽略了一些非常简单的事情。
有人可以指出我正确的方向吗?
最佳答案
你想做类似的事情
function Player() {
// player things here
}
Player.prototype = new SuperPlayer(); // get all the things on SuperPlayer prototype
Player.prototype.constructor = Player;
假设SuperPlayer确实是Player的超类。
编辑-如果SuperPlayer是更好的播放器,即Player的子类,则只需反转上述模式
function SuperPlayer() {
// super player things here
}
SuperPlayer.prototype = new Player(); // get all the things on Player prototype
SuperPlayer.prototype.constructor = SuperPlayer; // the above line changed the constructor; change it back
从您写的内容中我看不出SuperPlayer是否是子类。另外,其他答案也指出由于注释,您张贴的代码在语法上已损坏。