最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In what circumstances is it allowed to have a function body be an expression NOT surrounded by curly brackets? - St

programmeradmin3浏览0评论

I try to understand how Protovis works, and I stumbled upon code like this:

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

I tried rolling my own short functions, like this:

(function(a) a+2)

I am NOT asking about anonymous functions declared like function(){stuff();}. The code in question looks like function() stuff; and it works. I want to know why. I don't want to learn about constructs like myvar = function(a){return a+1;}, but about constructs like myvar = (function(a) a+1). Please look at the above code more carefully.

But, as I suspected, it threw a syntax error.

How can such code work?

(Note: the protovis code does work as intended.)

I try to understand how Protovis works, and I stumbled upon code like this:

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

I tried rolling my own short functions, like this:

(function(a) a+2)

I am NOT asking about anonymous functions declared like function(){stuff();}. The code in question looks like function() stuff; and it works. I want to know why. I don't want to learn about constructs like myvar = function(a){return a+1;}, but about constructs like myvar = (function(a) a+1). Please look at the above code more carefully.

But, as I suspected, it threw a syntax error.

How can such code work?

(Note: the protovis code does work as intended.)

Share Improve this question edited Oct 10, 2024 at 9:53 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Nov 17, 2010 at 16:30 Gabi PurcaruGabi Purcaru 31.6k9 gold badges80 silver badges96 bronze badges 1
  • 1 I don't get it. I even checked the grammar specified in the spec - the braces are required for anonymous functions: ecma-international/publications/files/ECMA-ST/ECMA-262.pdf "A.5 Functions and Programs". – user395760 Commented Nov 17, 2010 at 16:39
Add a ment  | 

3 Answers 3

Reset to default 10

This is a Expression Closure that was introduced in JavaScript 1.8. It is an extension to ECMAScript.

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

Protovis also has its own code to handle the case where the browser you're running does not yet support the Expression Closure format, here: http://vis.stanford.edu/protovis/jsdoc/symbols/src/src_pv-internals.js.html

This is an Expression Closure, see this:

  • http://asserttrue.blogspot./2009/07/javascript-expression-closures-and-why.html

Here's the jsfiddle that works:

  • http://jsfiddle/fyB9h/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论