【发布时间】:2010-11-17 16:30:42
【问题描述】:
我试图了解Protovis 的工作原理,但偶然发现了这样的代码:
force.node.add(pv.Dot)
.size(function(d) (d.linkDegree + 4) * Math.pow(this.scale, -1.5)) // notice this
.fillStyle(function(d) d.fix ? "brown" : colors(d.group)) // and this
.strokeStyle(function() this.fillStyle().darker()) // and even this
.lineWidth(1)
.title(function(d) d.nodeName)
.event("mousedown", pv.Behavior.drag())
.event("drag", force);
我尝试滚动自己的短函数,如下所示:
(function(a) a+2)
我不询问声明为function(){stuff();} 的匿名函数。有问题的代码看起来像 function() stuff; 并且可以工作。我想知道为什么。我不想了解myvar = function(a){return a+1;} 之类的构造,而是了解myvar = (function(a) a+1) 之类的构造。请仔细看上面的代码。
但是,正如我所怀疑的,它引发了语法错误。
这样的代码如何工作?
(注意:protovis 代码确实按预期工作。)
【问题讨论】:
-
我不明白。我什至检查了规范中指定的语法 - 匿名函数需要大括号 :ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf "A.5 Functions and Programs"。
标签: javascript function syntax