자바스크립트 함수 1
Javascript 2011. 12. 7. 14:21
"자바스크립트" 언어 에서 함수는 객체입니다.
이유는 아래와 같습니다.
- alert(typeof function() { return this; }); // function
-
alert(typeof new Function('', 'return this')); // function
-
-
alert(function() { return this; }.constructor); // Function()
-
alert(new Function('', 'return this').constructor); // Function()
typeof function() { return this; } 와 typeof new Function('', 'return this') 코드 모두 함수 객체를 생성한다는 것을 알 수 있습니다.
즉, function() { return this; }.constructor 와
new Function('', 'return this').constructor의 생성자 함수는 Function()이며, 둘다 함수 객체 라는 것을 다시
한번 알 수 있습니다.
두 객체 생성 방식의 차이점으로는 첫번째 함수 객체는 암묵적으로 생성되었고 두번째 함수 객체는 명시적으로 new 연산자를 통해 생성되었다는 것입니다.