-1

基本的に、オブジェクト内のいくつかの属性を一緒に追加するメソッドを使用してオブジェクトを作成しました。しかし、コンソールログのメソッドを呼び出そうとすると、返されることを期待していた値ではなく、コード(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ステートメントを返します

4

2 に答える 2

5

メソッドを呼び出すのではなく、console、log()への関数の参照を提供します。JavaScriptでは、関数を呼び出すために「()」を使用する必要があります。

これは確かに機能しますconsole.log(granite1.groupPrice());

サイドthis.price

を使用します this.groupPrice()。それ以外のgroupPrice

これを変更しました、価格メソッド

 this.price = function(){
        if(length <= 2000 && length > 1000){
            return ((this.edgeProfile + this.groupPrice())*2) - addOns;
        }else if(length <= 3000 && length > 2000){
            return ((this.edgeProfile + this.groupPrice())*3) - addOns;         
        }else if(length <= 4000 && length > 3000){
            return ((this.edgeProfile + this.groupPrice())*4) - addOns;         
        }else if(length <= 5000 && length > 4000){
            return ((this.edgeProfile + this.groupPrice())*5) - addOns;         
        }
    }
于 2012-11-12T14:46:24.360 に答える
4

関数を呼び出す場合は、()を追加します。それ以外の場合は、関数を参照しているだけです。

于 2012-11-12T14:46:19.950 に答える