【发布时间】:2011-06-02 19:58:15
【问题描述】:
我一开始是这样定义一个类的:
function mapTile(nx,ny)
{
//members
this.x = nx;
this.y = ny;
//methods
this.prototype.visible = function(){return true;};
this.prototype.getID = function(){return y*tiles_per_line+x;};
this.prototype.getSrc = function(){return 'w-'+this.getID+'.png';}
};
当我尝试创建对象时会引发异常:
t=new mapTile(1,1)
TypeError: Cannot set property 'visible' of undefined
在 Chromium 中并在 Firefox 中静默失败(使用 firebug)
这工作正常:
function mapTile(nx,ny)
{
//members
this.x = nx;
this.y = ny;
};
//methods
//this.prototype.xx=1;
mapTile.prototype.visible = function(){return true;};
在主体内部实现原型方法的正确方法是什么?
【问题讨论】:
标签: javascript oop prototype