본문 바로가기
javascript

First-class Function

by rami_ 2022. 1. 24.

 

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, ① a function can be passed as an argument to other functions, ② can be returned by another function and ③can be assigned as a value to a variable.

 

① a function can be passed as an argument to other functions

function sayHello() {
   return "Hello, ";
}
function greeting(helloMessage, name) {
  console.log(helloMessage() + name);
}
// Pass `sayHello` as an argument to `greeting` function
greeting(sayHello, "JavaScript!");
// Hello, JavaScript!

greeting함수에는 2개의 파라미터를 받고 있는데 그중 sayHello를 파라미터로 넣음. 이건 함수.

 

② can be returned by another function

function sayHello() {
   return function() {
      console.log("Hello!");
   }
}

자바스크립트에서 익명함수를 value로 처리했기 때문에 함수를 반환할 수 있음.

함수를 반환하는건 고차함수(Higher Order Function)

 

can be assigned as a value to a variable.

const foo = function() {
   console.log("foobar");
}
foo(); // Invoke it using the variable
// foobar

 

반환된 함수도 호출하기 위해 이중괄호를 사용함.

 

일급객체의 조건.

1. 인자로 다른함수에 전달

2. 반환값으로 다른 함수에 전달

3. 변수에 할당하거나 자료구조에 저장하거나

 

출처 : https://developer.mozilla.org/en-US/docs/Glossary/First-class_Function

'javascript' 카테고리의 다른 글

자료구조 Data Structure  (0) 2022.01.24
lexical grammer/어휘문법  (0) 2022.01.24
closure  (0) 2022.01.23
변수, type, 할당, return  (0) 2022.01.23
기본개념5. Object  (0) 2022.01.09