function sum(x, y, 콜백함수){ 콜백함수(x + y); return x + y; }function documentWriter(s){
document.write('콜백함수', s);
}sum(10, 20, documentWriter);
// map 의 사용을 통한 콜백함수
let arr = [1, 2, 3, 4, 5];
arr.map(제곱)function 제곱(x){
return x ** 2
}arr.map(x => x ** 2)
// forEach의 사용을 통한 콜백함수
let arr = [1, 2, 3, 4, 5];
arr.forEach(e => console.log(e**2));
function 제곱(x){
console.log(x**2)
}
function factorial(n) { if (n <= 1) return 1; return n * factorial(n - 1); }
console.log(factorial(5)); // 120
// 익명 즉시 실행 함수 (function () { let a = 1; let b = 2; return a + b; })();// 기명 즉시 실행 함수
(function foo() {
let a = 3;
let b = 5;
return a * b;
})();
foo(); // ReferenceError: foo is not defined
// 어차피 실행하지 못해서 의미가 없음.
// 메모리 효율적으로 관리하기 위해 바로 실행해야 하는 것들을 즉시 실행함수로 관리
function outer() { let count = 0; return function() { return ++count; }; }
let counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
'new' 연산자를 붙여 실행. new 연산자는 생성자 함수의 this 가 인스턴스를 바라보도록 만들어주는 역할.동일한 프로퍼티를 가지는 객체 생성
prototype을 이용하여 메모리 효율을 높일 수 있음
function Person(name, age) { this.name = name; this.age = age; }
let person1 = new Person('김철수', 30);
console.log(person1.name); // '김철수'