생성자 함수를 즉시실행함수로 감싸는 이유가 궁금합니다.
해결됨
모던 자바스크립트 딥다이브 스터디
예제25-01을 보면 생성자를 즉시실행함수로 감싸는 패턴이 있던데요. var Person = (function(){ function Person(name) { this.name = name; } Person.prototype.sayHi = function(){ console.log('Hi! My name is ' + this.name); }; return Person; })(); var me = new Person('Lee'); me.sayHi(); 굳이 이렇게 하는 이유는 무엇인가요? 아래와 같이 그냥 일반 함수 선언문으로 해도 될것 같은데요. function Person(name) { this.name = name; } Person.prototype.sayHi = function(){ console.log('Hi! My name is ' + this.name); } var me = new Person('Lee'); me.sayHi(); 혹시 함수 호이스팅? 때문에 그런거면 변수에 생성자를 할당해면 될것 같은데요... 즉시실행함수를 쓰는 이유를 도무지 모르겠네요; var Person = function Person(name) { this.name = name; } Person.prototype.sayHi = function(){ console.log('Hi! My name is ' + this.name); } var me = new Person('Lee'); me.sayHi();
- 즉시실행함수
- javascript





