基本上我已经创建了一个对象,该对象使用一种方法将对象中的多个属性添加在一起。但是当我尝试将该方法调用到控制台日志时,它会向我发出代码(这是一个 if 语句),而不是我希望它返回的值,所以我很困惑!为什么会这样?下面的代码:
var Granite = function(ty, gr, th, wi, le, ed, ad){
this.type = ty;
this.group = gr;
this.thickness = th;
this.width = wi;
this.length = le;
this.edgeProfile = ed;
this.addOns = ad;
this.groupPrice = function(){
if (thickness === 20){
switch(group)
{
case 1:
return 160;
break;
case 2:
return 194;
break;
case 3:
return 244;
break;
case 4:
return 288;
break;
case 5:
return 336;
break;
case 6:
return 380;
break;
default:
return 380;
}
}else{
switch(group)
{
case 1:
return 200;
break;
case 2:
return 242;
break;
case 3:
return 305;
break;
case 4:
return 360;
break;
case 5:
return 420;
break;
case 6:
return 475;
break;
default:
return 475;
}
}
}
this.price = function(){
if(length <= 2000 && length > 1000){
return ((edgeProfile + groupPrice)*2) - addOns;
}else if(length <= 3000 && length > 2000){
return ((edgeProfile + groupPrice)*3) - addOns;
}else if(length <= 4000 && length > 3000){
return ((edgeProfile + groupPrice)*4) - addOns;
}else if(length <= 5000 && length > 4000){
return ((edgeProfile + groupPrice)*5) - addOns;
}
}
}
var granite1 = new Granite("Rosa Porrino", 1, 30, 400, 3200, 30.05, 86.18);
console.log(granite1.groupPrice);
它将 groupPrice 方法中的完整 if 语句返回给我