gpt4 book ai didi

javascript - 奇怪的 Javascript 表达式

转载 作者:数据小太阳 更新时间:2023-10-29 03:48:56 25 4
gpt4 key购买 nike

我试图理解如何 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 代码确实按预期工作。)

最佳答案

这是 JavaScript 1.8 中引入的表达式闭包。它是 ECMAScript 的扩展。

https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.8

关于javascript - 奇怪的 Javascript 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4206685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com