카테고리 없음

유효범위

객체를 전역변수로 만들고 객체에 대한 속성으로 변수를 관리하는 방법
MYAPP = {}
MYAPP.calculator = {
'left' : null,
'right' : null
}
MYAPP.coordinate = {
'left' : null,
'right' : null
}
MYAPP.calculator.left = 10;
MYAPP.calculator.right = 20;
function sum(){
return MYAPP.calculator.left + MYAPP.calculator.right;
}
document.write(sum());


익명함수 호출법  --  전역변수를 사용하지 않아도된다.

(function(){
var MYAPP = {}
MYAPP.calculator = {
'left' : null,
'right' : null
}
MYAPP.coordinate = {
'left' : null,
'right' : null
}
MYAPP.calculator.left = 10;
MYAPP.calculator.right = 20;
function sum(){
return MYAPP.calculator.left + MYAPP.calculator.right;
}
document.write(sum());
}())

자바스크립트에서 일반적으로 위와같은방법으로 로직을 모듈화하한다


정적유효범위

함수가 선언된 시점에서만 유효범위를 가짐

var i = 5;
function a(){
var i = 10;
b();
}
function b(){
document.write(i);
}
a();

실행결과는 5