[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Arrow_functions [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 ..

this ( this, arguments, super, new.target). , , .

In this article

js
(param1, param2, , paramN) => { statements }
(param1, param2, , paramN) => expression
// : (param1, param2, , paramN) => { return expression; }

//       :
(singleParam) => { statements }
singleParam => { statements }

//       :
() => { statements }
() => expression
// : () => { return expression; }

js
//     ,    
params => ({foo: bar})

//       
(param1, param2, ...rest) => { statements }
(param1 = defaultValue1, param2, , paramN = defaultValueN) => { statements }

//   
var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
f();  // 6

"ES6 In Depth: Arrow functions" on hacks.mozilla.org.

: this.

. :

js
var elements = ["Hydrogen", "Helium", "Lithium", "Beryllium"];

elements.map(function (element) {
  return element.length;
}); //     [8, 6, 7, 9]

//       :
elements.map((element) => {
  return element.length;
}); // [8, 6, 7, 9]

//         return,
//   return    

elements.map((element) => element.length); // [8, 6, 7, 9]

//   ,      length,     :
//  ,   `"length"`  ,    ,
//     `lengthFooBArX`    ,      
elements.map(({ length: lengthFooBArX }) => lengthFooBArX); // [8, 6, 7, 9]

//       ,   .   ,  ,
//    `"length"`,  ,     .     ,
//      ,      `length`
elements.map(({ length }) => length); // [8, 6, 7, 9]

this

, this ( , undefined strict , " " ..). - .

js
function Person() {
  //   Person() `this`   .
  this.age = 0;

  setInterval(function growUp() {
    //   ,   growUp() `this` 
    //   ,    `this`,
    //    Person().
    this.age++;
  }, 1000);
}

var p = new Person();

ECMAScript 3/5, this :

js
function Person() {
  var that = this;
  that.age = 0;

  setInterval(function growUp() {
    //    (callback)   that, 
    //     this.
    that.age++;
  }, 1000);
}

, , this ( growUp() ).

this, this . :

js
function Person() {
  this.age = 0;

  setInterval(() => {
    this.age++; // `this`    Person
  }, 1000);
}

var p = new Person();

this , (strict mode) this :

js
var f = () => {
  "use strict";
  return this;
};
f() === window; //   

.

call apply

this , call() apply(), , this:

js
var adder = {
  base: 1,

  add: function (a) {
    var f = (v) => v + this.base;
    return f(a);
  },

  addThruCall: function (a) {
    var f = (v) => v + this.base;
    var b = {
      base: 2,
    };

    return f.call(b, a);
  },
};

console.log(adder.add(1)); //  2
console.log(adder.addThruCall(1)); //    2

arguments

arguments, arguments .

js
var arguments = 42;
var arr = () => arguments;

arr(); // 42

function foo() {
  var f = (i) => arguments[0] + i; //    arguments
  //   f
  // c  arguments  foo
  return f(2);
}

foo(1); // 3

arguments :

js
function foo() {
  var f = (...args) => args[0];
  return f(2);
}

foo(1); // 2

, . , , :

js
"use strict";
var obj = {
  i: 10,
  b: () => console.log(this.i, this),
  c: function () {
    console.log(this.i, this);
  },
};
obj.b(); // prints undefined, Window {...} (  )
obj.c(); // prints 10, Object {...}

("bind") this. Object.defineProperty():

js
"use strict";
var obj = {
  a: 10,
};

Object.defineProperty(obj, "b", {
  get: () => {
    console.log(this.a, typeof this.a, this);
    return this.a + 10;
    //    'Window',  'this.a'  'undefined'
  },
});

new

new:

js
var a = new (function () {})();
//  "a"      

var b = new (() => {})();
//   
// Uncaught TypeError: (intermediate value) is not a constructor

yield

yield ( , , ). .

(concise body) (block body) .

, .

js
var func = (x) => x * x; //  ,
//   
var func = (x, y) => {
  return x + y;
}; //  ,
//   

()

, : params => {object:literal} , .

js
var func = () => { foo: 1 };
//  func()  undefined!

var func = () => { foo: function() {} };
// SyntaxError: function statement requires a name

({}) (.. foo , ).

.

js
var func = () => ({ foo: 1 });

.

js
var func = ()
           => 1;
// SyntaxError: expected expression, got '=>'

, (), , .

js
let callback;

callback = callback || function() {}; // ok

callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments

callback = callback || (() => {});    // ok

js
//     undefined
let empty = () => {};

(() => "foobar")();
//  "foobar"
// ( Immediately Invoked Function Expression
//  'IIFE'  )

var simple = (a) => (a > 15 ? 15 : a);
simple(16); // 15
simple(10); // 10

let max = (a, b) => (a > b ? a : b);

//    : filter, map, ...

var arr = [5, 6, 13, 0, 1, 18, 23];

var sum = arr.reduce((a, b) => a + b);
// 66

var even = arr.filter((v) => v % 2 == 0);
// [6, 0, 18]

var double = arr.map((v) => v * 2);
// [10, 12, 26, 0, 2, 36, 46]

//    promise-
promise
  .then((a) => {
    // ...
  })
  .then((b) => {
    // ...
  });

//    ,    
setTimeout(() => {
  console.log("  ");
  setTimeout(() => {
    // deeper code
    console.log("  ");
  }, 1);
}, 1);

Specification
ECMAScript 2027 LanguageSpecification
# sec-arrow-function-definitions

Firefox


Web Proxy Viewer  |  New URL  |  Original Page