Protovis - 这些没有花括号的函数是什么? [英] Protovis - What are these functions with no curly braces?

查看:25
本文介绍了Protovis - 这些没有花括号的函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
JavaScript 中不带花括号的 Lambda 函数语法

处理 Protovis - 他们实现了一些没有花括号的奇怪委托函数 - 有人可以帮我遮一下吗?示例:

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

推荐答案

一般,如问题@missingno 链接到,这是用于声明函数的替代语法,主要由Firefox 支持.而不是:

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" };

你省略了大括号和 return 语句:

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 ()).

在 Protovis 中,在很多情况下,您需要声明简短的单语句匿名函数以作为参数传递给方法调用.这是一种常见的模式,该库包含一个解析实用程序,以确保上述语法在 Firefox 以外的浏览器中受支持.如果您将 Protovis 代码包含在这样的脚本标签中:

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>

脚本将由 Protovis 解析器进行评估,以确保支持特殊语法.

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

我的两分钱:这种语法的优点是它非常快(加上所有示例都使用它).使用 Protovis 的典型脚本涉及 lot 匿名函数,因此这可以为您节省一些输入,而且看起来非常棒.当我第一次开始使用 Protovis 时,我经常使用它 - 不仅在方法调用中,而且在变量声明中也是如此.

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:

  • 因为你所有的代码都是通过 Protovis 解析器运行的,它本质上是修改代码以重新添加 return 语句,然后 eval() 它,调试简单的语法错误变得非常困难.您会收到所有这些指向 Protovis 代码中的 eval() 行的意外标识符"错误,但没有指出问题(缺少分号等)在您自己的代码中发生的位置.

  • 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.

如果您希望您的代码在 Firefox 之外运行,您必须将所有代码包含在特殊的 javascript+protovis 脚本标签中,这意味着没有外部文件.一旦您开始做任何稍微复杂的事情,您就真的希望大部分时间都保持脚本独立.

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.

这篇关于Protovis - 这些没有花括号的函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆