팀 러버덕 by Hoon
const increase = function (num) {
return ++ num;
}
const decrease = function (num) {
return -- num;
}
const predicates = { increase, decrease }
function counter(predicate) {
let num = 0;
return function() {
num = predicate(num);
return num
}
}
const increase = counter(predicates.increase);
const increase = function (num) {
return ++ num;
}
const decrease = function (num) {
return -- num;
}
const predicates = { increase, decrease }
function counter(predicate) {
let num = 0;
return function() {
num = predicate(num);
return num
}
}
const increase = counter(predicates.increase);
따라서 함수도 프로퍼티를 가질 수 있다!
function square(number) {
return number * number
}
console.dir(square)
ƒ square(number)
arguments: null
caller: null
length: 1
name: "square"
prototype: {constructor: ƒ}
[[FunctionLocation]]: VM127:1
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[1]
따라서 함수도 프로퍼티를 가질 수 있다!
function square(number) {
return number * number
}
console.log(Object.getOwnPropertyDescriptors(square))
/*
arguments: {value: null, writable: false, enumerable: false, configurable: false}
caller: {value: null, writable: false, enumerable: false, configurable: false}
length: {value: 1, writable: false, enumerable: false, configurable: true}
name: {value: 'square', writable: false, enumerable: false, configurable: true}
prototype: {value: {…}, writable: true, enumerable: false, configurable: false}
[[Prototype]]: Object
*/
따라서 함수도 프로퍼티를 가질 수 있다!
function multiply(num1, num2) {
console.log(arguments);
return num1 * num2;
}
console.log(multiply(3, 6))
/*
0: 3
1: 6
callee: ƒ multiply(num1, num2)
length: 2
Symbol(Symbol.iterator): ƒ values()
[[Prototype]]: Object
18
*/
가변 인자 함수를 구현할때 유용!
function sum() {
let result = 0;
for (let i = 0; i < arguments.length; i ++) {
result += arguments[i]
}
return result
}
function myFunction(func) {
return func()
}
function calledFunction() {
return 'caller: ' + calledFunction.caller;
}
console.log(myFunction(calledFunction))
/*
caller: function myFunction(func) {
return func()
}
*/
console.log(calledFunction())
// caller: null