[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

*

20157

*

JavaScript (first-class)Function

JavaScript

JavaScript FunctionFunction

return undefined return (new)

()

js
/*  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 }
name

param

statements

(function expression)

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

param

statements

js
var myFunction = function () {
  // statements
};

js
var myFunction = function namedFunction() {
  // statements
};

function function

IIFE

js
(function () {
  statements;
})();

IIFE

(function* )

(function* statement )

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

param

statements

(function*)

( function* expression ):

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

param

statements

(=>)

( arrow functions ):

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

() ( foo => 1)

statements or expression

statements expression

Function

Function JS

Function new

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

JavaScript x,theValuea,b

functionBody

JavaScript

Function ( new ) Function

GeneratorFunction ().

(GeneratorFunction constructor) JS

GeneratorFunction new

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

JavaScript x,theValuea,b

functionBody

JavaScript

Function ( new ) Function

arguments

argumentsarguments

Getter setter

getter() setter() getters setters

get

set

ECMAScript 6 getters setters method definitions .

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

vs vs

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_namedmultiply

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

var undefined

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

Function's toString method

'new Function' SpiderMonkey JavaScript "anonymous" alert(new Function())

js
function anonymous() {}

anonymous

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

Function

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

Function ()

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}"

  • source element
js
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
  }
}

js
// 
function foo() {}

// 
(function bar() {});

// 
x = function hello() {};

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

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

ECMAScript 6 ECMAScript 6

js
"use strict";

function f() {
  return 1;
}

{
  function f() {
    return 2;
  }
}

f() === 1; // true

// f() === 2 in non-strict mode

js
if (shouldDefineZero) {
  function zero() {
    // DANGER: 
    console.log("This is zero.");
  }
}

ECMAScript 6 shouldDefineZero false zero zero

ECMAScript 6 shouldDefineZero true zero

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

0

js
//  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

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

typeof window noFunc

js
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