Nev*_*ane 2 javascript oop jquery
我正在创建一个函数对象.但是,当我运行此对象时,它返回undefined.
例如,在这个JSFiddle示例中,当我尝试时alert(test(8)),函数运行并返回13,但是当我创建一个Function对象时,'alert(func(8))'返回undefined.
<button onclick="start()">Start Test</button>
<script>
    function test(num) {
        alert("running...");
        return num + 5;
    }
    function start(){
        alert(test(8));
        var func = new Function("num", "test(num)");
        alert(func(8)); 
    }
</script>
因为在你的第二个func,你没有return价值test(num)
function test(num) {
  alert("running...");
  return parseInt(num) + 5;
}
function start(){
  alert(test(8));
  // return the value obtained on calling test(num)     <------------
  var func = new Function("num", "return test(num)");
  alert(func(8));
}
start();| 归档时间: | 
 | 
| 查看次数: | 82 次 | 
| 最近记录: |