Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Expression closures

这篇翻译不完整。请帮忙从英语翻译这篇文章

Non-standard. Do not use!
The expression closure syntax is a deprecated SpiderMonkey-specific feature and will be removed. For future-facing usages, consider using arrow functions.

表达式闭包是定义简单函数的一种便捷方式。

Syntax 语法

function [name]([param1[, param2[, ..., paramN]]])
   expression

Parameters 参数

name
函数名。函数名可以省略不写,称为匿名函数。函数名仅在函数体有效。
paramN
形参名。一个函数最多可以有255个参数。
expression
构成函数体的表达式。

Description 描述

This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical Lambda notation.

JavaScript 1.7 以上:

function(x) { return x * x; }

JavaScript 1.8:

function(x) x * x

该语法支持省略花括号和'return'语句。使用这种编码的目的只是为了在句法上使得代码更加简化,但除此之外没有其他好处。

Examples 例子

一种绑定事件监听器的便捷方式:

 document.addEventListener("click", function() false, true);

Using this notation with some of the array functions from JavaScript 1.6:

elems.some(function(elem) elem.type == "text");

Browser compatibility 浏览器兼容

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 未实现 (Yes) 未实现 未实现 未实现
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 未实现 未实现 (Yes) 未实现 未实现 未实现

Gecko specific note

Starting with Gecko / SpiderMonkey 45 (Firefox 45 / Thunderbird 45 / SeaMonkey 2.42), console warnings are added when expression closures are used (bug 995610).

See also

文档标签和贡献者

 此页面的贡献者: mizhon, webery
 最后编辑者: mizhon,