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

Revision 707001 of Expression closures

  • Revision slug: Web/JavaScript/Reference/Operators/Expression_closures
  • Revision title: Expression closures
  • Revision id: 707001
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

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.
{{jsSidebar("Operators")}}

Summary

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

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • {{jsxref("Functions_and_function_scope", "Functions and function scope")}}
  • {{jsxref("Function")}}
  • {{jsxref("Statements/function", "function statement")}}
  • {{jsxref("Operators/function", "function expression")}}
  • {{jsxref("Statements/function*", "function* statement")}}
  • {{jsxref("Operators/function*", "function* expression")}}
  • {{jsxref("GeneratorFunction")}}

Revision Source

<div class="warning">
 <strong>Non-standard. Do not use!</strong><br />
 The expression closure syntax is a deprecated SpiderMonkey-specific feature and <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1083458">will be removed</a>. For future-facing usages, consider using <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow functions</a>.</div>
<div>
 <div>
  {{jsSidebar("Operators")}}</div>
</div>
<h2 id="Summary">Summary</h2>
<p>Expression closures are a shorthand function syntax for writing simple functions.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
function [<em>name</em>]([<em>param1</em>[, <em>param2[</em>, ..., <em>paramN</em>]]])
   <em>expression</em>
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>name</code></dt>
 <dd>
  The function name. Can be omitted, in which case the function is <em>anonymous</em>. The name is only local to the function body.</dd>
 <dt>
  <code>paramN</code></dt>
 <dd>
  The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd>
 <dt>
  <code>expression</code></dt>
 <dd>
  The expression which comprise the body of the function.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical <a class="external" href="https://en.wikipedia.org/wiki/Lambda_calculus#Lambda_calculus_and_programming_languages">Lambda notation</a>.</p>
<p>JavaScript 1.7 and older:</p>
<pre class="brush: js">
function(x) { return x * x; }</pre>
<p>JavaScript 1.8:</p>
<pre class="brush: js">
function(x) x * x</pre>
<p>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.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<p>A shorthand for binding event listeners:</p>
<pre class="brush: js">
 document.addEventListener("click", function() false, true);
</pre>
<p>Using this notation with some of the array functions from JavaScript 1.6:</p>
<pre class="brush: js">
elems.some(function(elem) elem.type == "text");
</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Functions_and_function_scope", "Functions and function scope")}}</li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Statements/function", "function statement")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Statements/function*", "function* statement")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("GeneratorFunction")}}</li>
</ul>
Revert to this revision