[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/function [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 2015 ..

function .

Function .

In this article

var myFunction = function [name]([param1[, param2[, ..., paramN]]]) {
   statements
};

ES2015 .

name

. , . .

paramN

, .

statements

, .

. , . IIFE (Immediately Invoked Function Expression), , . .

JavaScript (hoisting), . , .

js
console.log(notHoisted); // undefined
//    ,    
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function () {
  console.log("bar");
};

, . ( ). , arguments.callee.

js
var math = {
  factit: function factorial(n) {
    console.log(n);
    if (n <= 1) {
      return 1;
    }
    return n * factorial(n - 1);
  },
};

math.factit(3); //3;2;1;

, , name, . . , name ( ). , ( ). ( , .. ).

js
var foo = function () {};
foo.name; // "foo"

var foo2 = foo;
foo2.name; // "foo"

var bar = function baz() {};
bar.name; // "baz"

console.log(foo === foo2); // true
console.log(typeof baz); // undefined
console.log(bar === baz); // false (errors because baz == undefined)

() x. :

js
var x = function (y) {
  return y * y;
};

-.

js
button.addEventListener("click", function (event) {
  console.log("button is clicked!");
});

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page