【问题标题】:Javascript Prototyping ErrorJavascript 原型设计错​​误
【发布时间】:2010-10-17 21:08:14
【问题描述】:

我最近一直在研究 Javascript 原型,可以理解理论,并相信它对我理解语言非常有用,但不能完全让以下工作......

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

每个编译器/检查器都会在第 2 行标记一个“缺少分号”错误。 我很难过,但相信我忽略了一些非常简单的事情。

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

【问题讨论】:

  • 那是您使用的确切代码吗?该注释正在创建语法错误。

标签: javascript class inheritance prototype


【解决方案1】:

你想做类似的事情

function Player() {
    // player things here

}

Player.prototype = new SuperPlayer(); // get all the things on SuperPlayer prototype
Player.prototype.constructor = Player;

假设 SuperPlayer 是 Player 的超类。

edit -- 如果 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 是否是一个子类。此外,其他答案指出,由于评论,您发布的代码在语法上被破坏了......

【讨论】:

  • 其实反过来。 :} 我在思考“超级”的思路,暗指增强,而不是基础。对不起!
【解决方案2】:

引擎不知道super_playerfunction,直到您将其声明为一个,因此它没有prototype

var player = function () {},
    super_player = function () {}

// now we can happily set the prototype :)
super_player.prototype = new player();

// don't forget to point the constructor back to super_player
// not doing so will cause great confusion
super_player.prototype.constructor = super_player

【讨论】:

    【解决方案3】:

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

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

    如果我是你,我认为你想考虑使用驼峰式类名来执行以下操作:

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

    这将使 SuperPlayer 成为基类,而 Player 成为派生类,因为 Player 通过它的原型从 SuperPlayer 继承。不像你在上面的例子中那样。

    【讨论】:

    • 我将不得不更详细地研究您的答案。更重要的是,感谢您了解我的内联注释生成的语法错误。它不在我的工作示例中,但是当我在这里发帖时我应该知道得更好! ^_^
    【解决方案4】:

    你想说

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

    ?

    【讨论】:

      【解决方案5】:

      再次查看chubbards的答案...原型关键字可以最好地描述为笨拙...这很奇怪,因为它肯定是OOP,但实现很疯狂!

      举个常见的例子……

      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? :}
      

      对吗? :|

      【讨论】:

      • 你写的只是一个结构,而不是Javascript。如果你觉得原型继承概念很奇怪(很多人也是如此),那么你可以使用其他 Javascript 库,例如 ExtJS 或原型,它们提供了以更自然的方式声明对象层次结构的方法。原型是函数的属性。它不能单独使用。所以你必须有:cat.prototype = new哺乳动物(),或者tiger.prototype = new cat()。使用 Ext.JS 之类的东西,你可以做 cat.extends(mama) 之类的事情。在幕后,它的作用大致相同。
      • 我们只有 600 个字符,因此很难做出回应。我会尽量简短。 Javascript 使用函数不仅可以定义常规函数,还可以定义类等概念。以大写字母开头的函数表示构造函数。当我们编写'new SomeObject()'时。 new 关键字分配一个空对象,将 this 关键字设置为该新对象,然后调用函数 SomeObject。 SomeObject 然后可以使用 this.someMember 语法为该对象提供数据成员。从而使 SomeObject 像一个构造函数...
      • 现在原型是每个函数都有的属性。此属性形成一个链,对其进行属性解析。当在对象上引用属性时,它首先在该对象内部查找属性,如果该属性不存在,则在原型中查找具有相同名称的属性,并继续沿着原型链向上直到找到它.它模仿了相同的面向对象语言如何解决继承层次结构中的成员。我们创建一个 new SomeObject() 的原因是为了初始化在该构造函数中创建的任何实例变量。
      【解决方案6】:

      非常尊重你 Chubbard。 :)

      你的例子很有帮助。

      我最初的“示例”相当轻率。 :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.pounce() 可以工作,而 lion.pounce() 失败了?

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

      我有很多 Javascript 问题...这是一种非常奇怪的语言... ;)

      【讨论】:

        猜你喜欢
        • 2016-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        相关资源
        最近更新 更多