// 1. 함수는 무명의 리터럴로 생성할 수 있다 // 2. 함수는 변수에 저장할 수 있다 // 런타임에 함수 리터럴이 평가되어 함수 객체가 생성되고 변수가 할당된다 const increase = function (num) { return ++num; }; const decrease = function (num) { return --num; }; // 2. 함수는 객체에 저장할 수 있다 const auxs = { increase, decrease }; // 3. 함수의 매개변수에 전달할 수 있다 // 4. 함수의 반환값으로 사용할 수 있다 function makeCounter(aux) { let num = 0; return function () { num = aux(num); return num; }; } // 3. 함수는 매개변수에게 함수를 전달할 수 있다. const increaser = makeCounter(auxs.increase); console.log(increaser()); // 1 console.log(increaser()); // 2 // 3. 함수는 매개변수에게 함수를 전달할 수 있다. const decreaser = makeCounter(auxs.decrease); console.log(decreaser()); // -1 console.log(decreaser()); // -2
function square(number) { return number * number; } console.dir(square);
function multiply(x, y) { console.log(arguments); return x * y; } console.log(multiply()); //NaN console.log(multiply(1)); //NaN console.log(multiply(1, 2)); //2 console.log(multiply(1, 2, 3)); //2
undefined
로 초기화된 상태를 유지한다function foo() {} console.log(foo.length); // 0 function bar(x) {} console.log(bar.length); // 1
const obj = { a: 1 }; // 객체 리터럴 방식으로 생성한 객체의 프로토타입 객체는 Object.prototype이다 console.log(obj.__proto__ === Object.prototype); // true // 객체 리터럴 방식으로 생성한 객체는 프로토타입 객체인 Object.prototype의 프로퍼티를 상속받는다 // hasOwnProperty 메서드는 Object.prototype의 메서드다 console.log(obj.hasOwnProperty('a')); // true console.log(obj.hasOwnProperty('__proto__'); // false
// 함수 객체는 prototype 프로퍼티를 소유한다 (fuction () {}).hasOwnProperty('prototype'); // true // 일반 객체는 prototype 프로퍼티를 소유하지 않는다 ({}).hasOwnProperty('prototype'); // false