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.

Expression closures are a shorthand function syntax for writing simple functions.

Syntax

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

Parameters

name
The function name. Can be omitted, in which case the function is anonymous. The name is only local to the function body.
paramN
The name of an argument to be passed to the function. A function can have up to 255 arguments.
expression
The expression which comprise the body of the function.

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 and older:

function(x) { return x * x; }

JavaScript 1.8:

function(x) x * x

This syntax allows you to leave off the braces and 'return' statement - making them implicit. There is no added benefit to writing code in this manner, other than having it be syntactically shorter.

Examples

A shorthand for binding event listeners:

 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 No support (Yes) No support No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support (Yes) No support No support No support

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

문서 태그 및 공헌자

 이 페이지의 공헌자: fscholz, arai
 최종 변경: fscholz,