See also
// function문
> function add1(a, b) {
return a + b;
}
// 함수 리터럴
> var add2 = function (a, b) {
return a + b;
};
> add1(1, 2)
3
> add2(1, 2)
3
> function err1() {
undef(); // 정의되지 않은 함수 호출
};
> err1();
ReferenceError: undef is not defined
at err1 ([object Context]:2:1])
// 생략
> var err2 = function () {
undef();
};
> err2();
ReferenceError: undef is not defined
at [object Context]:2:1
// 생략
See also
> function factorial(n) {
if (n == 1) { return 1; }
return n * factorial(n - 1); // no tail recursion
}
> factorial(4);
24
> var scope = "global";
> function f() {
console.log(scope);
var scope = "local";
console.log(scope);
}();
undefined // not "global"
local
> var scope = "global";
> function f() {
var scope;
console.log(scope);
scope = "local";
console.log(scope);
}();
> var counter = function () {
var value = 0;
return {
get: function () {
return ++value;
}
};
}();
> counter.get();
1
> counter.get();
2
> function helloWorld() {
console.log('hello world!');
}
> setTimeout(helloWorld, 1000);
hello world! // 1초 뒤 출력
> var counter = function () {
var value = 0;
return function () {
return ++value;
};
}();
> counter();
1
> counter();
2