| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions | [Back] [Original] |
Get to know MDN better
JavaScript FunctionFunction
()
/* myFunc */
function myFunc(theObject) {
// mycar theObject
theObject.brand = "Toyota";
}
/*
* mycar;
* ;
* mycar
*/
var mycar = {
brand: "Honda",
model: "Accord",
year: 1998,
};
/* 'Honda' */
window.alert(mycar.brand);
/* */
myFunc(mycar);
/*
* 'Toyota',
*/
console.log(mycar.brand);
this arguments.callee (****)
function name([param[, param[, ... param]]]) { statements }
function expression)var myFunction = function name([param[, param[, ... param]]]) { statements }
var myFunction = function () {
// statements
};
var myFunction = function namedFunction() {
// statements
};
function function
(function () {
statements;
})();
IIFE
function* )function* name([param[, param[, ...param]]]) { statements }
function*) ( function* expression ):
function* [name]([param] [, param] [..., param]) { statements }
( arrow functions ):
([param] [, param]) => { statements } param => expression
param () ( foo => 1)
statements or expressionstatements expression
Function Function JS
Function new
new Function (arg1, arg2, ... argN, functionBody)
arg1, arg2, ... argNJavaScript x,theValuea,b
functionBodyJavaScript
Function ( new ) Function
(GeneratorFunction constructor) JS
new GeneratorFunction (arg1, arg2, ... argN, functionBody)
arg1, arg2, ... argNJavaScript x,theValuea,b
functionBodyJavaScript
Function ( new ) Function
argumentsargumentsarguments
getter() setter() getters setters
ECMAScript 6 getters setters method definitions .
var obj = {
foo() {},
bar() {},
};
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_namedmultiply
var multiply = function func_name(x, y) {
return x * y;
};
var undefined
var y = function x() {};
alert(x); // throws an error
'new Function' SpiderMonkey JavaScript "anonymous" alert(new Function())
function anonymous() {}
anonymous
var foo = new Function("alert(anonymous);");
foo();
Function
foo(); // alerts FOO!
function foo() {
alert("FOO!");
}
Function ()
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; // source element
if (x === 0) {
// source element
x = 10; // source element
function boo() {} // source element
}
function foo() {
// source element
var y = 20; // source element
function bar() {} // source element
while (y === 10) {
// source element
function blah() {} // source element
y++; // source element
}
}
//
function foo() {}
//
(function bar() {});
//
x = function hello() {};
if (x) {
//
function world() {}
}
//
function a() {
//
function b() {}
if (0) {
//
function c() {}
}
}
"use strict";
function f() {
return 1;
}
{
function f() {
return 2;
}
}
f() === 1; // true
// f() === 2 in non-strict mode
if (shouldDefineZero) {
function zero() {
// DANGER:
console.log("This is zero.");
}
}
ECMAScript 6 shouldDefineZero false zero zero
ECMAScript 6 shouldDefineZero true zero
var zero;
if (0) {
zero = function () {
console.log("This is zero.");
};
}
0
// 0
function padZeros(num, totalLen) {
var numStr = num.toString(); //
var numZeros = totalLen - numStr.length; // zeros
for (var i = 1; i <= numZeros; i++) {
numStr = "0" + numStr;
}
return numStr;
}
padZeros
var result;
result = padZeros(42, 4); // returns "0042"
result = padZeros(42, 2); // returns "42"
result = padZeros(5, 4); // returns "0005"
typeof window noFunc
if ("function" === typeof window.noFunc) {
// use noFunc()
} else {
// do something else
}
if noFunc
| ECMAScript 2027 LanguageSpecification # sec-function-definitions |
| Web Proxy Viewer | New URL | Original Page |