// 함수도 객체처럼 프로퍼티 갖을 수 있음
function add(x, y) {
return x + y;
}
add.result = add(3, 2);
add.status = 'OK';
console.log(add.result); // (출력값) 5
console.log(add.status); // (출력값) ‘OK’
객체라는 것은 함수도 일반 객체처럼 취급될 수 있다를 의미합니다.
자바스크립트에서 함수는 ’일급 객체’로서 아래와 같은 동작이 가능합니다.
// 변수에 함수 할당
var foo = 100;
var bar = function() {
return 100;
};
console.log(bar()); // (출력값) 100
// 프로퍼티에 함수 할당
var obj = {};
obj.baz = function() {
return 200;
}
console.log(obj.baz()); // (출력값) 200
// 함수를 다른 함수의 인자로 넘긴 코드
var foo = function(func) {
func(); // 인자로 받은 func() 함수 호출
}
foo(function() {
console.log('Function can be used as the argument.');
});
var foo = function() {
return function() {
console.log(‘this function is the return value.‘);
}
}
var bar = foo();
bar(); // (출력값) this function is the return value.
ㅇ