This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var o = { | |
message: (function() { | |
var who = "me", what = "call"; | |
alert(1); | |
return what + " " + who; }()), | |
getMsg: function() { | |
return this.message; | |
} | |
}; | |
//o로드와 동시에 alert 실행 | |
o.getMsg(); //call me | |
o.message; //call me |
객체의 프로퍼티가 객체의 생명주기 안에서는 값이 변하지 않고,
처음 값을 정의하기 위해서 뭔가 계산이 필요하다고 하면.. 위와 같이 즉시실행 함수를 사용한다.
이 함수는 스크립트 로딩시에 실행되어 프로퍼티를 정의해놓게된다.