2014-09-18 95 views
0

我在這段代碼中有一個對象,帶有一個我希望能夠運行的方法列表。 TS和TL是從我的網站輸入的可變整數。我想要做的就是使這些var的存儲數量來自每種方法中的等式。我誤解了什麼樣的回報?我是否需要以某種方式指定我希望我的循環遍歷方法?我究竟做錯了什麼?不能使對象方法返回var

所以大家都明白這一點。該對象正被用於計算測量結果,但取決於用戶在TL和TS中輸入的內容取決於方程式等於​​什麼。之後,我需要將每個變量中的數字轉換爲一個字符串,然後將其拆分(小數點),使用Erik Garrison的分數庫將小數轉換爲分數,然後將分數與原始方程中的整數相加。

function Cut_Lengths(){ 
var SideWall_Retainer; 
var EndWall_Retainer; 
var SideWall_Coping; 
var EndWall_Coping; 
var Housing_Shells; 
var Housing_Pre_Cap; 
var Housing_Cap; 
var Housing_Anchor_Plate; 
var Housing_Bond_Beam_Plate; 
var Flush_Lid; 
var Flush_Lid_Fascia; 


var cutLengths = { 
    SWRT : function(){SideWall_Retainer = TL + 10.75; return SideWall_Retainer;}, 
    EWRT : function(){EndWall_Retainer = TS - .1875; return EndWall_Retainer;}, 
    SWCP : function(){SideWall_Coping = TL + 7.625; return SideWall_Coping;}, 
    EWCP : function(){EndWall_Coping = TS - .5; return EndWall_Coping;}, 
    NHS : function(){Housing_Shells = Math.floor((TS + 48)/72); return Housing_Shells;}, 
    HPC : function(){Housing_Pre_Cap = TS + 48; return Housing_Pre_Cap}, 
    HC : function(){Housing_Cap = TS + 46.25; return Housing_Cap;}, 
    HAP : function(){Housing_Anchor_Plate = TS - .25; return Housing_Anchor_Plate;}, 
    HBP : function(){Housing_Bond_Beam_Plate = TS - .1875; return Housing_Bond_Beam_Plate;}, 
    FL : function(){Flush_Lid = TS + 13; return Flush_Lid;}, 
    FLF : function(){Flush_Lid_Fascia = TS - .5; return Flush_Lid_Fascia;} 
}; 
for (var x in cutLengths){}; 
} 
+0

你在期待什麼?一切都很好,但只是...不完整。 – sahbeewah 2014-09-18 21:03:53

回答

0

你的函數的這個版本將應用正確的計算,這要看你傳爲typeTLTS值。 (如果要使用「外部」值,則可以在函數簽名中省略TLTS)。不需要循環變量。

function Cut_Lengths(type, TL, TS){ 
    var cutLengths = { 
     SWRT : function(){ return TL + 10.75; }, 
     EWRT : function(){ return TS - .1875; }, 
     SWCP : function(){ return TL + 7.625; }, 
     EWCP : function(){ return TS - .5; }, 
     NHS : function(){ return Math.floor((TS + 48)/72); }, 
     HPC : function(){ return TS + 48; }, 
     HC : function(){ return TS + 46.25; }, 
     HAP : function(){ return TS - .25; }, 
     HBP : function(){ return TS - .1875; }, 
     FL : function(){ return TS + 13; }, 
     FLF : function(){ return TS - .5; } 
    }; 
    return cutLengths[type](); 
} 
+0

完美!謝謝。你介意解釋爲什麼在我原來的功能回報不起作用?我在做什麼這是錯的? – dm8021122 2014-09-18 21:18:38

+0

根本沒有'return'。 ;)雖然'cutLength'對象的函數會返回一些東西,*(a)*它們永遠不會被調用,並且*(b)* Cut_Lengths()函數本身根本不會做任何事情(除了空循環)。 – lxg 2014-09-18 21:21:57

+0

應該是'return cutLengths [type]();'得到結果。 – PHPglue 2014-09-18 21:29:13

0

如果你想在一個循環中執行各功能是這樣的:

for(var i in cutLengths){ 
    // i is property like SWRT -> cutLengths[i] is value which is method in your case 
    var revolvingVariable = cutLengths[i](); 
} 

現在在循環revolvingVariable的每一步都是在循環的該步驟的函數的返回值。我想你想使用一個構造函數,雖然:

function CutLengths(TL, TS){ 
    // you can leave out arguments if you want 
    this.currentTL = TL ? TL : 1; // change 1 to default 
    this.currentTS = TS ? TS : 1; // change 1 to default 
    this.end_wall_container_value; 
    this.side_wall_retainer = function(){ 
    return this.currentTL + 10.75; 
    } 
    /* The keyword `this` refers to the Object or current instance created with 
    the keyword `new` outside of `this` Constructor, `CutLengths`. 
    Must be a method since `this.currentTS` could change and a property gets 
    it's value when you assign it. 
    */ 
    this.end_wall_container = function(set){ 
    if(set){ 
     this.end_wall_container_value = set; 
     return set; 
    } 
    var ts = this.currentTS - 0.1875; 
    this.end_wall_container_value = ts; 
    return ts; 
    } 
    this.side_wall_coping = function(){ 
    return this.currentTL + 7.625; 
    } 
    this.end_wall_coping = function(){ 
    return this.currentTS - 0.5; 
    } 
    this.housing_shells = function(){ 
    return Math.floor((this.currentTS + 48)/72); 
    } 
    this.housing_pre_cap = function(){ 
    return this.currentTS + 48; 
    } 
    this.housing_cap = function(){ 
    return this.currentTS + 46.25; 
    } 
    this.housing_anchor_plate = function(){ 
    return this.currentTS - 0.25; 
    } 
    this.housing_bond_beam_plate = function(){ 
    return this.currentTS - 0.1875; 
    } 
    this.flush_lid = function(){ 
    return this.currentTS + 13; 
    } 
    this.flush_lid_fascia = function(){ 
    return this.currentTS - 0.5; 
    } 
} 
var cutL = new CutLengths(1, 2); 
console.log(cutL.end_wall_container()); 
cutL.currentTL = 5; 
console.log(cutL.end_wall_container()); 
console.log(cutL.end_wall_container(25)); 
console.log(cutL.end_wall_container()); 
var cutL_another = new CutLengths; 
console.log(cutL_another.end_wall_container()); 
+0

你的循環中的評論是超級有用的!非常感謝你。 – dm8021122 2014-09-18 21:15:03