我最近一直在研究Javascript原型,可以理解这个理论,并且相信它对我对语言的理解非常有用,但不能完全得到以下的工作......
var player = function(){//Declarations here};
var super_player.prototype = new player();
每个编译器/检查器在第2行标记“缺少分号”错误。 我很难过,但相信我忽略了一些非常简单的东西。
有人能指出我正确的方向吗?
答案 0 :(得分:3)
您想要做类似
的事情function Player() {
// player things here
}
Player.prototype = new SuperPlayer(); // get all the things on SuperPlayer prototype
Player.prototype.constructor = Player;
假设SuperPlayer是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是子类,我无法判断你写的是什么。此外,其他答案指出,由于评论...
,您发布的代码在语法上已被破坏答案 1 :(得分:2)
引擎不知道super_player
是function
,直到您将其声明为一个,因此它没有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
答案 2 :(得分:2)
您对第1行的评论阻止了结束括号。所以你有一个悬挂的开放式支架,不起作用。您可以将其更改为以下内容:
var player = function() {
// Declarations here
};
如果我是你,我想你想考虑使用驼峰案例类名做以下事项:
function SuperPlayer() {
}
function Player() {
}
Player.prototype = new SuperPlayer();
Player.prototype.constructor = SuperPlayer;
这将使SuperPlayer成为基类,而Player作为Player的派生类通过它的原型从SuperPlayer继承。不像你上面的例子那样反过来。
答案 3 :(得分:0)
你想说吗
super_player.prototype.player = function(){ /*Declarations here*/ };
答案 4 :(得分:0)
再次查看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? :}
对吗? :|
答案 5 :(得分:0)
非常尊重你的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.pounce()borks时,为什么lion.prototype.pounce()可以工作?
拿破仑和罗里都是狮子会毕竟...
我加载了更多Javascript问题......这是一种非常奇怪的语言......;)