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

* Some parts of this feature may have varying levels of support.

, "", ( , ) . , , . , .

JavaScript , : . , Function.

JavaScript.

In this article

JavaScript Function. Function Function.

. , , .

, , return, , . . , new, this. undefined.

. . , . , , , , .

js
/*   'myFunc' */
function myFunc(theObject) {
  theObject.brand = "Toyota";
}

/*
 *   'mycar';
 *     Object;
 *  'mycar'    
 */
var mycar = {
  brand: "Honda",
  model: "Accord",
  year: 1998,
};

/*  'Honda' */
console.log(mycar.brand);

/*       */
myFunc(mycar);

/*
 *  'Toyota',     'brand'
 *    .
 */
console.log(mycar.brand);

this , , Function , .

:

( function)

( : function statement):

function name([param[, param[, ... param]]]) {
   statements
}
name

.

param

, . 255 .

statements

, .

- ( function)

- ( : function operator):

function [name]([param] [, param] [..., param]) {
   statements
}
name

. , .

param

, . 255 .

statements

, .

- (=>)

: *,* ECMAScript 6 .

, this ( ):

([param] [, param]) => {
   statements
}

param => expression
param

. , (). , ().

statements or expression

, {}. , ( ` x => 3 + 8 11).```

Function

: Function , , , JavaScript, .

Function new ( ):

new Function (arg1, arg2, ... argN, functionBody)
arg1, arg2, ... argN

. , JavaScript. , . : "x", "theValue", "a,b".

functionBody

, .

Function new, .

: , ECMAScript 6, .

, , undefined. .

. .

arguments

arguments.

( ) ( ) , . .

get

, .

set

, .

: , ECMAScript 6, .

ECMAScript 6, , , . .

js
var obj = {
  foo() {},
  bar() {},
};

Function -

:

, Function multiply:

js
var multiply = new Function("x", "y", "return x * y");

multiply:

js
function multiply(x, y) {
  return x * y;
}

-, multiply:

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

- func_name, multiply:

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

, :

, . , , , . - , . ( , undefined). :

js
var y = function x() {};
alert(x); //  

- , Function.toString.

, , , , .

, , , . (function declaration) , . :

  1. - (function expression), .
  2. (function declaration), , .

, 'new Function', . , JavaScript SpiderMonkey, , "anonymous". , , alert(new Function()) :

js
function anonymous() {}

, anonymous . , :

js
var foo = new Function("alert(anonymous);");
foo();

, - Function, , , , . :

js
foo(); //  FOO!
function foo() {
  alert("FOO!");
}

, -, , . , Function, , ( ).

, - , , . , Function, . - , , , - , "new Function(...)". Function , .

, - , Function, . :

js
var foo = new Function(
  "var bar = 'FOO!';\nreturn(function() {\n\talert(bar);\n});",
)();
foo(); //  "function() {\n\talert(bar);\n}"   ,   ,     .

( ) -. , :

  • " " . - :
js
var x = 0; //  
if (x == 0) {
  //  
  x = 10; //   
  function boo() {} //   
}
function foo() {
  //  
  var y = 20; //  
  function bar() {} //  
  while (y == 10) {
    //  
    function blah() {} //   
    y++; //   
  }
}

js
//  
function foo() {}

// -
(function bar() {});

// -
x = function hello() {};

if (x) {
  // -
  function world() {}
}

//  
function a() {
  //  
  function b() {}
  if (0) {
    // -
    function c() {}
  }
}

function ( ECMA-262 Edition 3) Function. , ES5 strict. , - , .

zero , 'if (0)' false:

js
if (0) {
  function zero() {
    document.writeln("This is zero.");
  }
}

'if (1)', zero .

, , , - ( ), . -.

JavaScript- ( SpiderMonkey), - . , zero , false. - :

js
if (0) {
  var zero = function () {
    document.writeln("This is zero.");
  };
}

:

, :

js
function padZeros(num, totalLen) {
  var numStr = num.toString(); //      
  var numZeros = totalLen - numStr.length; //     
  for (var i = 1; i <= numZeros; i++) {
    numStr = "0" + numStr;
  }
  return numStr;
}

padZeros:

js
var result;
result = padZeros(42, 4); //  "0042"
result = padZeros(42, 2); //  "42"
result = padZeros(5, 4); //  "0005"

:

, typeof. , window noFunc. , ; , - .

js
if ("function" == typeof window.noFunc) {
  //  noFunc()
} else {
  //  - 
}

, noFunc , .

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page