Javascript 在这种情况下如何继承对象?
这可能是以某种形式提出的问题,我试图检查此资源: My gameobject.js如下所示:Javascript 在这种情况下如何继承对象?,javascript,inheritance,Javascript,Inheritance,这可能是以某种形式提出的问题,我试图检查此资源: My gameobject.js如下所示: function GameObject(program) { this.program = program; this.graphics = null; } GameObject.prototype.update = function(dt) { } GameObject.prototype.draw = function() { // Here graphics woul
function GameObject(program)
{
this.program = program;
this.graphics = null;
}
GameObject.prototype.update = function(dt)
{
}
GameObject.prototype.draw = function()
{
// Here graphics would be the graphics of the child class,
// because the graphics would be replaced with the child class' graphics
if(this.graphics !== null)
this.program.renderer.render(this.graphics);
}
var box = new Box(program, 10, 10, 100, 100);
// In game class update method
box.update(this.td);
// In game class draw method
box.draw();
function Box(program, width, height, x, y)
{
var self = this;
this.base = new GameObject(program);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function(dt) { self.base.update(dt); };
this.draw = this.base.draw();
}
我想要另一个类,例如box.js:
function Box(program, width, height, x, y)
{
// Call base constructor here?
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
现在我想基本上继承GameObject的update和draw方法,并用程序参数调用GameObject构造函数,这样最终事情应该是这样的:
function GameObject(program)
{
this.program = program;
this.graphics = null;
}
GameObject.prototype.update = function(dt)
{
}
GameObject.prototype.draw = function()
{
// Here graphics would be the graphics of the child class,
// because the graphics would be replaced with the child class' graphics
if(this.graphics !== null)
this.program.renderer.render(this.graphics);
}
var box = new Box(program, 10, 10, 100, 100);
// In game class update method
box.update(this.td);
// In game class draw method
box.draw();
function Box(program, width, height, x, y)
{
var self = this;
this.base = new GameObject(program);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function(dt) { self.base.update(dt); };
this.draw = this.base.draw();
}
所以基本上就像在C#中一样。如果我能让这个盒子继承游戏对象的更新和绘制方法,这将有很大帮助
在此处编辑1:JSFIDLE:
编辑2:我尝试过这样的解决方法:
function GameObject(program)
{
this.program = program;
this.graphics = null;
}
GameObject.prototype.update = function(dt)
{
}
GameObject.prototype.draw = function()
{
// Here graphics would be the graphics of the child class,
// because the graphics would be replaced with the child class' graphics
if(this.graphics !== null)
this.program.renderer.render(this.graphics);
}
var box = new Box(program, 10, 10, 100, 100);
// In game class update method
box.update(this.td);
// In game class draw method
box.draw();
function Box(program, width, height, x, y)
{
var self = this;
this.base = new GameObject(program);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function(dt) { self.base.update(dt); };
this.draw = this.base.draw();
}
因此,每次创建一个长方体时,我都会创建一个基类GameObject的新实例,然后将长方体的更新和绘制方法设置为GameObject的更新和绘制方法
但这并没有起到作用,我认为这种方法无论如何都有严重的问题
编辑3:也许我应该像以前那样做。。。从GameObject继承的所有内容仍然必须覆盖更新和绘制方法。只是我想我不能确定gameobject列表中的每个对象都有draw和update方法,我只能假设它们有。JavaScript中的构造函数只是普通函数。要从
框中调用GameObject
,您可以使用:
GameObject.call(this, program);
要使Box
对象继承表单GameObject
使用
Object.setPrototypeOf(Box.prototype, GameObject.prototype);
确保在第一次调用new Box
之前将该行放在上面
为了与旧浏览器(tanks@Bergi)保持最佳兼容性,您可以使用新对象覆盖Box.prototype
,而不是使用object.setPrototypeOf
Box.prototype = Object.create(GameObject.prototype, { configurable: true, writable: true, value: Box });
然后您应该能够在框
对象上使用绘制
和更新
方法
下面的例子应该会让你走上正确的道路
功能游戏对象(程序)
{
这个程序=程序;
this.graphics=null;
}
GameObject.prototype.update=函数(dt)
{
警报(“更新”);
}
GameObject.prototype.draw=函数()
{
//这里的图形将是子类的图形,
//因为图形将替换为子类的图形
如果(this.graphics!==null)
this.program.renderer.render(this.graphics);
}
功能框(程序、宽度、高度、x、y)
{
调用(这个,程序);
这个。宽度=宽度;
高度=高度;
这个.x=x;
这个。y=y;
}
setPrototypeOf(Box.prototype,GameObject.prototype);
设b=新框();
b、 更新()
那么您希望游戏对象
继承自框
?从另一个方向看一下@Bergi No。Box应该继承GameObject,然后Box应该覆盖GameObject的绘制和更新方法。这样我就可以制作一个游戏对象列表,并在列表中放置框和东西,确保列表中的每个对象都有绘制和更新方法。@Bergi我想我真的不应该把JavaScript看作是基于类的OOP,因为如果没有变通方法和垫片,它似乎无法工作。好吧,反过来说,应该没有区别。但请注意,在JS中创建异构列表并不需要继承。是的,您当然可以使用基于类的方法(毕竟类是一个非常有用的概念!),但是您需要记住,您是在用原型OOP实现这个概念。请注意,Object.create
比Object.setPrototypeOf
受支持得多,如果我在Box构造函数中执行this.graphics=new graphics()
,draw函数会认为它不再为null吗?@Bergi实际上Object.setPrototypeOf
在较旧的Safari浏览器中不受支持。无论如何,如果Box.prototype
被覆盖,则需要手动将Box.prototype.constructor
指定为自己的属性。@当然可以,如果您在调用GameObject
@GOTO0后执行此操作:任何旧浏览器都不支持它,如果可用,则需要使用\uuuuuuuu protoo\uo
对其进行填充<代码>对象。创建
只需要ES5,这就是我的意思。如果您使用ES6,您最好立即使用class
:-)