| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
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 Function. Function Function.
. , , .
, , return, , . . , new, this. undefined.
. . , . , , , , .
/* '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, .
argumentsarguments: , , .arguments.callee : , .arguments.caller : , .arguments.length: , .( ) ( ) , . .
: , ECMAScript 6, .
ECMAScript 6, , , . .
var obj = {
foo() {},
bar() {},
};
Function -:
, Function multiply:
var multiply = new Function("x", "y", "return x * y");
multiply:
function multiply(x, y) {
return x * y;
}
-, multiply:
var multiply = function (x, y) {
return x * y;
};
- func_name, multiply:
var multiply = function func_name(x, y) {
return x * y;
};
, :
, . , , , . - , . ( , undefined). :
var y = function x() {};
alert(x); //
, , , , .
, , , . (function declaration) , . :
, 'new Function', . , JavaScript SpiderMonkey, , "anonymous". , , alert(new Function()) :
function anonymous() {}
, anonymous . , :
var foo = new Function("alert(anonymous);");
foo();
, - Function, , , , . :
foo(); // FOO!
function foo() {
alert("FOO!");
}
, -, , . , Function, , ( ).
, - , , . , Function, . - , , , - , "new Function(...)". Function , .
, - , Function, . :
var foo = new Function(
"var bar = 'FOO!';\nreturn(function() {\n\talert(bar);\n});",
)();
foo(); // "function() {\n\talert(bar);\n}" , , .
( ) -. , :
var x = 0; //
if (x == 0) {
//
x = 10; //
function boo() {} //
}
function foo() {
//
var y = 20; //
function bar() {} //
while (y == 10) {
//
function blah() {} //
y++; //
}
}
//
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:
if (0) {
function zero() {
document.writeln("This is zero.");
}
}
'if (1)', zero .
, , , - ( ), . -.
JavaScript- ( SpiderMonkey), - . , zero , false. - :
if (0) {
var zero = function () {
document.writeln("This is zero.");
};
}
, :
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:
var result;
result = padZeros(42, 4); // "0042"
result = padZeros(42, 2); // "42"
result = padZeros(5, 4); // "0005"
, typeof. , window noFunc. , ; , - .
if ("function" == typeof window.noFunc) {
// noFunc()
} else {
// -
}
, noFunc , .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-function-definitions |
This page was last modified on 27 . 2025 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |