无法使对象方法返回var

时间:2014-09-18 20:57:40

标签: javascript loops return

我在此代码中有一个对象,其中包含我希望能够运行的方法列表。 TS和TL是从我的站点输入的变量整数。我想要做的就是让这些var存储每个方法中由等式产生的数字。我误解了回报是什么吗?我是否需要以某种方式指定我希望我的循环运行方法?我做错了什么?

所以每个人都明白这一点。该对象用于计算测量值,但取决于用户在TL和TS中输入的内容将取决于等式的等值。之后我需要将每个变量中的数字转换为字符串,在小数点处将split()转换为小数点,使用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){};
}

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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]();
}