Javascript原型错误

Javascript原型错误,javascript,class,inheritance,prototype,Javascript,Class,Inheritance,Prototype,我最近一直在研究Javascript原型,能够理解其原理,并且相信它对我理解该语言非常有用,但无法完全实现以下功能 var player = function(){//Declarations here}; var super_player.prototype = new player(); 每个编译器/检查器在第2行标记“缺少分号”错误。 我很困惑,但我相信我忽略了一些非常简单的事情 有人能给我指出正确的方向吗?你想说什么 super_player.prototype.player = fu

我最近一直在研究Javascript原型,能够理解其原理,并且相信它对我理解该语言非常有用,但无法完全实现以下功能

var player = function(){//Declarations here};
var super_player.prototype = new player();
每个编译器/检查器在第2行标记“缺少分号”错误。 我很困惑,但我相信我忽略了一些非常简单的事情

有人能给我指出正确的方向吗?

你想说什么

super_player.prototype.player = function(){ /*Declarations here*/ };

引擎不知道super_player是一个函数,直到您将其声明为一个函数,因此它没有原型


您在第1行的评论阻止了结束大括号。所以你有一个悬空的开放支架,它不起作用。您可以将其更改为以下内容:

var player = function() {
   // Declarations here
};
function SuperPlayer() {
}

function Player() {
}
Player.prototype = new SuperPlayer();
Player.prototype.constructor = SuperPlayer;
如果我是你,我认为你想考虑使用CAMEL CASE类名做如下:

var player = function() {
   // Declarations here
};
function SuperPlayer() {
}

function Player() {
}
Player.prototype = new SuperPlayer();
Player.prototype.constructor = SuperPlayer;

这将使SuperPlayer成为基类,而Player则作为其原型从SuperPlayer继承而来的派生类。与上面的示例不同。

您想做类似的事情

function Player() {
    // player things here

}

Player.prototype = new SuperPlayer(); // get all the things on SuperPlayer prototype
Player.prototype.constructor = Player;
假设超级玩家是超级玩家,可以这么说

编辑-如果SuperPlayer是一个更好的播放器,即播放器的子类,只需反转上述模式即可

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是否为子类。另外,其他答案指出,由于评论,您发布的代码在语法上被破坏…

再次查看了chubbards的答案。。。prototype关键字可以最好地描述为笨拙。。。这很奇怪,因为它肯定是面向对象的,但是实现是疯狂的

举一个普通的例子

mammal() = object;
cat() = prototype; // Implements mammal();
tiger() = object; // Implements cat(), but does NOT require the 'prototype' keyword... like some kind of 'cystalline' form? I can then create a whole range of individual 'tigers', but will not be able to add any new properties to any of them...
这个怎么样

machine() = object;
car() = prototype; // Implements machine. It's a machine afterall...
super_car = prototype; // It's a very fast car...
ferrari = prototype; // They only make supercars...
ferrari_355 = object; // One of the best cars they've ever made. No need to change it, right? :}

我说得对吗|

非常尊敬你,胖子:

你的例子帮助很大

我最初的“例子”相当轻率:P

不过我还是遇到了一些问题。。。 要详细说明我的“猫”样本,请尽量简短:

function mammal(){
// 'mammals' constructor - My 'empirical' 'class'...
} 
   mammal.count = 0; // To keep track of the zoo I've created. No subclass can access this property. It's a 'static' property

   // I could have declared this 'method' inside of my constructor
   // with an anonymous function attached to (this.prototype.breathe).

    mammal.track = function (){
        this.count++;
    }

    mammal.prototype.breathe = function(){
        alert("Inhale... Exhale...");
    }

function cat(){
// 'cat' constructor
}

// All cats shall now become a type of mammal
cat.prototype = new mammal();

cat.prototype = function(){
// This anonymous function is the REAL constructor for my 'cat' 'superclass'
}

    cat.prototype.meow = function(){
        alert("Meow!");
    }

function lion(){
// The king of jungle...
// I can keep track of the number of 'mammal' instances I create here
mammal.track();
}

// All lions are cats, afterall...
lion.prototype = new cat();
// Also note that I have no plans to extend the lion class.
// I have no need of a class below the 'idea' of a lion 

    lion.name = "Kitty"; // :}

    // Here's where I get confused...
    // I can set (lion.name) via instances, can't call (lion.pounce), but (lion.prototype.roar) works all day long! o_0
    lion.pounce = function(){
        alert(this.name+" pounces...")
    }

    lion.prototype.roar = function(){
        alert(this.name+" doesn't meow, he ROOOAAARS!"); 
    }

// With these constructs in place, I can now script...
$(document).ready(function(){

      var rory = new lion();    
      var napoleon = new lion();

      alert("We have "+mammal.count+" mammals running about");

          // This is 'Rory'...
          rory.name = 'Rory'; // Respect the pun...
          rory.roar();

          // This is 'Napoleon'...
          napoleon.name = 'Napoleon';
          napoleon.breathe(); // Napoleon can't breathe... he didn't inherit mammal.prototype.breathe(), for some reason
          napoleon.roar(); // How am I able to set (lion.name), but not call lion.pounce()?
          napoleon.pounce();

});
你是对的,当然,我的链中的每个类,直到创建最终实例,都是一个原型函数。但是为什么lion.name有效,而lion.prototype.name无效呢。相反,为什么lion.prototype.pumpe会在lion.pumpe停止运行时工作

拿破仑和罗里毕竟都是狮子


我有更多的Javascript问题。。。这是一种非常奇怪的语言

这就是你使用的密码吗?注释正在创建语法错误。事实上,相反。:}我想的是“超级”,暗指的是一种增强,而不是一种基础。很抱歉我将不得不更详细地研究你的答案。更重要的是,感谢您了解我的内联评论产生的语法错误。这不在我的工作示例中,但当我在这里发布时,我应该更清楚^_^您编写的只是一个结构,而不是Javascript。如果您发现原型继承概念和许多人一样奇怪,那么您可以使用其他Javascript库,如ExtJS或prototype,它们提供了以更自然的方式声明对象层次结构的方法。原型是功能的属性。它不能单独使用。所以你必须有:cat.prototype=新的哺乳动物,或者tiger.prototype=新的猫。使用Ext.JS之类的工具,您可以执行cat.JS之类的操作。在封面下,它做了大致相同的事情。很难回应,因为我们只有600个字符。我会尽量简短。Javascript使用函数不仅定义常规函数,还定义类等概念。以大写字母开头的函数表示作为构造函数的函数。当我们写“newsomeobject”时。new关键字分配一个空对象,将this关键字设置为该新对象,然后调用函数SomeObject。然后,SomeObject可以使用this.someMember语法为该对象提供数据成员。因此,使某个对象成为构造函数……现在原型是每个函数都具有的属性。此属性形成一个链,对其进行属性解析。在对象上引用属性时,它首先在该对象内部查找属性,如果该属性不存在,则会查找同名属性的原型,并沿着原型链向上继续查找,直到找到它为止。它模仿了相同的OO语言如何解析继承层次结构中的成员。我们创建新SomeObject的原因是,在该构造函数中创建的任何实例变量都会被初始化。