[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/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 7.

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

( ) " ". , . , .

JavaScript , . . .

JavaScript .

In this article

. Function . typeof "" "" .

: instanceof Function . , Function.prototype Function . Function.prototype . .

return return undefined . return . , .

: new .

MDN . .

js
function formatNumber(num) {
  return num.toFixed(2);
}

formatNumber(2);

num (parameter) , . JavaScript num . formatNumber(2) 2 (argument). , . arguments .

. , . , . .

js
function updateBrand(obj) {
  //        .
  obj.brand = "";
  //   
  //       .
  obj = null;
}

const car = {
  brand: "",
  model: "",
  year: 1994,
};

console.log(car.brand); // 

//    .
updateBrand(car);

// updateBrand  car   .
console.log(car.brand); // 

this , .

JavaScript .

.

, function*, async function, async function*

, function*, async function , async function*

Function(), GeneratorFunction(), AsyncFunction(), AsyncGeneratorFunction()

. Classes new Function.prototype typeof MyClass === "function" .

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

// 
function multiply(x, y) {
  return x * y;
} // No need for semicolon here

// ;    .
const multiply = function (x, y) {
  return x * y;
};
// ;   .
const multiply = function funcName(x, y) {
  return x * y;
};

//  
const multiply = (x, y) => x * y;

// 
const obj = {
  multiply(x, y) {
    return x * y;
  },
};

.

  • Function() , function , function , new . . , Generator Generator .
  • function . .
  • Function() . .
  • arguments this .
  • Function() .
  • Function() .

function . . , . . ( ). .

js
const y = function x() {};
console.log(x); //  : x  .

.

. .

new Function , . console.log(new Function().toString()) .

js
function anonymous(
) {

}

. Function() anonymous , . . .

js
new Function("alert(anonymous);")();

. , . Function .

js
// p  .
globalThis.p = 5;
function myFunc() {
  // p  .
  const p = 9;

  function decl() {
    console.log(p);
  }
  const expr = function () {
    console.log(p);
  };
  const cons = new Function("\tconsole.log(p);");

  decl();
  expr();
  cons();
}
myFunc();

// Logs:
// 9 ('decl'   ( ))
// 9 ('expr'   ( ))
// 5 ('cons'   ( ))

, Function . new Function(...) . Function .

.

js
//  .
function foo() {
  console.log("FOO!");
}

doSomething(
  //    .
  function foo() {
    console.log("FOO!");
  },
);

. expression statement function async function , IIFEs( ) .

js
function () { // SyntaxError: Function statements require a function name
  console.log("FOO!");
}();

function foo() {
  console.log("FOO!");
}(); // SyntaxError: Unexpected token ')'

function . void .

js
(function () {
  console.log("FOO!");
})();

void function () {
  console.log("FOO!");
}();

.

js
function myFunc(a, b, c) {
  // You can access the values of a, b, and c here
}

.

js
function myFunc({ a, b }, c = 1, ...rest) {
  //  a, b, c     .
}

.

arguments

arguments .

arguments

.

arguments.callee

.

arguments.length

.

Getter setter

. classes getter setter .

get

.

set

.

. Getter setter Object.getOwnPropertyDescriptor() API .

. ES2015 .

js
"use strict";

function f() {
  return 1;
}

{
  function f() {
    return 2;
  }
}

f() === 1; // true

//     f() === 2

, .

. .

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

. 0 if . shouldDefineZero zero . . .

.

js
// var      .
//       .
var zero;
if (shouldDefineZero) {
  zero = function () {
    console.log("zero.");
  };
}

0 .

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

padZeros .

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

typeof . window noFunc . , .

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

if noFunc , () .

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page