Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t使对象方法返回var_Javascript_Loops_Return - Fatal编程技术网

Javascript Can';t使对象方法返回var

Javascript Can';t使对象方法返回var,javascript,loops,return,Javascript,Loops,Return,我在这段代码中有一个对象,其中包含我希望能够运行的方法列表。TS和TL是从我的站点输入的可变整数。我所要做的就是让这些var存储每个方法中从方程得到的数字。我是否误解了return的功能?我是否需要以某种方式指定我希望循环通过这些方法运行?我做错了什么 所以大家都明白这一点。该对象用于计算测量值,但取决于用户在TL和TS中输入的内容,将取决于等式等于什么。然后,我需要将每个变量中的数字转换为字符串,在小数点处拆分(),使用Erik Garrison的分数库将小数转换为分数,并将分数与原始方程中的

我在这段代码中有一个对象,其中包含我希望能够运行的方法列表。TS和TL是从我的站点输入的可变整数。我所要做的就是让这些var存储每个方法中从方程得到的数字。我是否误解了return的功能?我是否需要以某种方式指定我希望循环通过这些方法运行?我做错了什么

所以大家都明白这一点。该对象用于计算测量值,但取决于用户在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){};
}

如果要在循环中执行每个函数,如下所示:

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());

此版本的函数将应用正确的计算,具体取决于传递给
类型
TL
TS
的值。(如果要使用“外部”值,也可以在函数签名中省略
TL
TS
。)无需循环变量

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]();
}

你在期待什么?一切看起来都很好,但是。。。不完整。你循环中的评论非常有用!非常感谢,太好了!非常感谢。你能解释一下为什么我原来函数中的返回不起作用吗?我所做的什么是错的?根本没有返回尽管
cutLength
对象的函数会返回一些东西,(a)它们从未被调用,(b)
Cut_length()
函数本身根本不做任何事情(除了空循环)。应该是
返回cutLength[type]()以获得结果。@PHPglue:谢谢!已修复。请确保在JavaScript中将
0
放在小数前面。