我想创建一个函数的数组,所以我可以调用一个取决于索引。 设置功能不起作用。 setF1和setF2函数可以。 以这种方式使事情发挥作用的设计决策是什么?为什么数组中的javascript函数不能使用?
您可以在jrootham.tjddev.net/test/test.html
我似乎无法粘贴在这里的测试代码中找到测试页。
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var thing =
{
f1 : function() {alert(1);},
f2 : function() {alert(2);},
setF1 : function() {this.call = this.f1;},
setF2 : function() {this.call = this.f2;},
list : [this.f1, this.f2],
set: function(index){this.call=this.list[index];},
call : this.f1
}
</script>
</head>
<body>
<button type="button" onClick="thing.set(0)">Set 0</button>
<button type="button" onClick="thing.set(1)">Set 1</button>
<button type="button" onClick="thing.setF1()">Set F1</button>
<button type="button" onClick="thing.setF2()">Set F2</button>
<button type="button" onClick="thing.call()">Call</button>
</body>
</html>
'{}'创建'object'; '[]'创建一个'数组'。两者之间存在显着差异。 – zzzzBov 2010-12-17 06:06:05