如何在类中运行函数。Javascript
我很难在类中调用函数。当我开始做这项工作时,我让它运行,但在做了修改后,我无法让它工作:(如何在类中运行函数。Javascript,javascript,function,this,Javascript,Function,This,我很难在类中调用函数。当我开始做这项工作时,我让它运行,但在做了修改后,我无法让它工作:( 您可以忽略我的警报,因为我只是将它们放在那里检查函数是否被调用。 我有一个函数“Unit”,它是我的一个类。然后我在下面调用它。 从下面的代码中可以看出,newunit(args)没有完成。它在this.direction=this.setDirection(); 由于某种原因,没有调用此.setDirection()。有人能看一下并告诉我出了什么问题吗? 谢谢 var teamBlack=[]; va
您可以忽略我的警报,因为我只是将它们放在那里检查函数是否被调用。
我有一个函数“Unit”,它是我的一个类。然后我在下面调用它。
从下面的代码中可以看出,
newunit(args)
没有完成。它在this.direction=this.setDirection();
由于某种原因,没有调用此.setDirection()。有人能看一下并告诉我出了什么问题吗?谢谢
var teamBlack=[];
var bking=新单位(“黑”、“王”、新坐标(3,1,假));
teamBlack.push(bking);
职能单位(团队、类型、协调人){
警报(“机组启动”+计数);
这个团队=团队;
this.type=type;
这个位置=坐标;
this.highlight=false;
警报(“指示前”);
this.direction=this.setDirection();
警报(“此”+此.type);
this.setDirection=函数(){
警报(“设置方向启动”);
警报(此.type);
var tempDir=[];
开关(此.type){
“国王”一案:
警惕(“这是国王”);
tempDir=[this.N()、this.NW()、this.W()、this.SW()、this.S()、this.SE()、this.E()、this.NE()];
打破
“bishop”案:
tempDir=[this.NW()、this.SW()、this.SE()、this.NE();
打破
案例“rook”:
tempDir=[this.N()、this.S()、this.W()、this.E();
打破
“典当”一案:
{
如果(this.team==“白色”){
tempDir=[this.S()];
}否则{
tempDir=[this.N()];
}
打破
}
“女王”案:
{
如果(this.team==“白色”){
tempDir=[this.N()、this.W()、this.SW()、this.S()、this.SE()、this.E()];
}否则{
tempDir=[this.N()、this.NW()、this.W()、this.S()、this.E()、this.NE()];
}
打破
}
}
tempDir=tempDir.filter(函数(dir){
返回dir.x>-1&&dir.x<3&&dir.y>-1&&dir.y<4&&dir.c==false;
});
返回tempDir;
}
}
此代码错误,因为您尝试调用类中尚不存在的函数。我建议您在prototype中移动此函数。例如:
function Unit() {
//this is your constructor
}
Unit.prototype.setDirection = function() {
//your code of setDirection function
}
此代码错误,因为您尝试调用类中尚不存在的函数。我建议您在prototype中移动此函数。例如:
function Unit() {
//this is your constructor
}
Unit.prototype.setDirection = function() {
//your code of setDirection function
}
确保定义了变量,然后在调用
this.setDirection()
之前放置this.setDirection()
请参阅。确保定义了变量,然后在调用
this.setDirection()
之前放置this.setDirection()
请参阅。我将通过分离责任来改进您的代码:
//create the class Unit
function Unit(team, type, coords) {
alert("Unit started " + count);
this.team = team;
this.type = type;
this.position = coords;
this.highlight = false;
}
然后为类指定新方法:
Unit.prototype.setDirection = function () {
alert("setDirection Started ");
alert(this.type);
var tempDir = [];
switch (this.type) {
case "king" :
alert("this is king");
tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()];
break;
case "bishop" :
tempDir = [this.NW(), this.SW(), this.SE(), this.NE()];
break;
case "rook" :
tempDir = [this.N(), this.S(), this.W(), this.E()];
break;
case "pawn" :
{
if (this.team == "white") {
tempDir = [this.S()];
} else {
tempDir = [this.N()];
}
break;
}
case "queen" :
{
if (this.team == "white") {
tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()];
} else {
tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()];
}
break;
}
}
tempDir = tempDir.filter(function (dir) {
return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false;
});
alert("before setdirection");
this.direction = tempDir;//setMethod doesn't return thing but set something somewhere
alert("this " + this.type);
};
我建议您查看一下我将通过分离职责来改进您的代码:
//create the class Unit
function Unit(team, type, coords) {
alert("Unit started " + count);
this.team = team;
this.type = type;
this.position = coords;
this.highlight = false;
}
然后为类指定新方法:
Unit.prototype.setDirection = function () {
alert("setDirection Started ");
alert(this.type);
var tempDir = [];
switch (this.type) {
case "king" :
alert("this is king");
tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()];
break;
case "bishop" :
tempDir = [this.NW(), this.SW(), this.SE(), this.NE()];
break;
case "rook" :
tempDir = [this.N(), this.S(), this.W(), this.E()];
break;
case "pawn" :
{
if (this.team == "white") {
tempDir = [this.S()];
} else {
tempDir = [this.N()];
}
break;
}
case "queen" :
{
if (this.team == "white") {
tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()];
} else {
tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()];
}
break;
}
}
tempDir = tempDir.filter(function (dir) {
return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false;
});
alert("before setdirection");
this.direction = tempDir;//setMethod doesn't return thing but set something somewhere
alert("this " + this.type);
};
我建议您查看一下您需要使用setDirection函数作为单元类的原型
Unit.prototype.setDirection = function() {
//setDirection functionality
}
在原型中,您可以通过this
访问类属性
检查此JSFIDLE:您需要使用setDirection函数作为单元类的原型
Unit.prototype.setDirection = function() {
//setDirection functionality
}
在原型中,您可以通过this
访问类属性
检查此JSFIDLE:在为this.setDirection赋值之前,您正在调用它。您不能这样做。请将其移到后面。您可能听说函数声明被“提升”,但您对setDirection的赋值不是一个声明,而是一个函数对对象属性的简单赋值。您在给它赋值之前调用了this.setDirection。您不能这样做。请将它移到后面。您可能听说过函数声明被“提升”,但您对setDirection的赋值不是一个声明,而是一个函数对对象属性的简单赋值。在您的行之后..sorr我忘记键入..修复在您的行之后..sorr我忘记键入..修复