我最近一直在研究 Javascript 原型(prototype),可以理解其理论,并相信它对我理解该语言非常有用,但无法完全让以下内容发挥作用......
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 的父类(super class)。
编辑 - 如果 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 是否是子类。另外,其他答案指出,由于评论,您发布的代码在语法上已被破坏......
关于JavaScript 原型(prototype)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955296/