JavaScript 原型(prototype)错误

标签 javascript class inheritance prototype

我最近一直在研究 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/

相关文章:

java - 没有获得扩展类中变量的正确值

python - 将类分配给变量

css继承,隐藏根元素

javascript - ajax中for循环中使用的.length和用于数字输出的.length有什么区别?

javascript - 使用 FB.ui 将预定义消息添加到 Facebook "Post to Your Wall"对话框

c++ - 我什么时候需要默认构造函数?

c# - 从一个基类继承两个类给出错误 3024

javascript - 比较日期时的位置

javascript - bootstrappedUser - Jade 或 EJS 到 HTML

java - 我的 Java 中的 ShapeApp 出现问题