Protovis - What are these functions with no curly braces? [duplicate]

夙愿已清 提交于 2019-12-10 17:01:35

问题


Possible Duplicate:
Lambda function syntax in JavaScript without curly braces

Dealing with Protovis - they implement some strange delegate functions which are given without curly braces - can someone shade a light on it for me, please? Example:

vis.add(pv.Label)
.data(cols)
.left(function() this.index * w + w / 2)
.top(0)
.textAngle(-Math.PI / 2)
.textBaseline("middle");

回答1:


In general, as explained in the question @missingno linked to, this is an alternate syntax for declaring functions, primarily supported by Firefox. Instead of:

function() { return "stuff" };

you omit the curly braces and return statement:

function() "stuff";

The end of the function occurs anywhere that a normal statement might end - a semicolon (;), a comma (,), or a close parenthesis ()).

In Protovis, there are a lot of cases where you need to declare short, one-statement anonymous functions to pass in as arguments to method calls. This is such a common pattern that that library includes a parsing utility to make sure that the above syntax is supported in browsers other than Firefox. If you enclose your Protovis code in script tags like this:

<script type="text/javascript+protovis">
// ...
</script>

the script will be evaluated by the Protovis parser, which ensures support for the special syntax.

My two cents on this: The upside of this syntax is that it's really fast (plus all the examples use it). A typically script using Protovis involves a lot of anonymous functions, so this can save you some typing, and it looks pretty awesome. When I first started using Protovis, I used it a lot - not just in method calls, but in variable declarations as well.

But, it has a few really heavy problems:

  • Because all your code is run through the Protovis parser, which essentially munges the code to re-add the return statements and then eval() it, it becomes fantastically hard to debug simple syntax errors. You get all these "Unexpected identifier" errors pointing to the eval() line in the Protovis code, with no indication of where the issue (a missing semicolon, etc) is occurring in your own code.

  • If you want your code to work outside Firefox, you have to include all your code in the special javascript+protovis script tags, which means no external files. Once you start doing anything of even marginal complexity, you really want to keep your scripts separate most of the time.

  • As with any "clever" syntax, it can get really hard to read, especially when you're using it in unexpected ways (e.g. outside of a method call). Yes, it's concise, but there's a definite price to pay in legibility.

All that said, I still use it when I want to make a rough sketch quickly. But most of the time, I'd suggest sticking to normal script tags and standard, curly-braced function declarations.



来源:https://stackoverflow.com/questions/6831655/protovis-what-are-these-functions-with-no-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!