반응형
1. 함수나 객체명의 중복을 피하기 위해서 네임스페이스를 적용한다.
var com;
if (!com) com = {};
else if (typeof com != "object") throw new Error("com already exists and is not an object");
if (!com.ahnseungkyu) com.ahnseungkyu = {}
else if (typeof com.ahnseungkyu != "object") throw new Error("com.ahnseungkyu already exists and is not an object");
2. closure를 사용하여 private 네임스페이스 정의
var com;
if (!com) com = {}
if (!com.david) com.david = {};
com.david.Class = {};
(function() {
var counter = 0;
function getCounter() { return counter; }
var ns = com.david.Class;
ns.getCounter = getCounter;
})();
var com;
if (!com) com = {};
else if (typeof com != "object") throw new Error("com already exists and is not an object");
if (!com.ahnseungkyu) com.ahnseungkyu = {}
else if (typeof com.ahnseungkyu != "object") throw new Error("com.ahnseungkyu already exists and is not an object");
2. closure를 사용하여 private 네임스페이스 정의
var com;
if (!com) com = {}
if (!com.david) com.david = {};
com.david.Class = {};
(function() {
var counter = 0;
function getCounter() { return counter; }
var ns = com.david.Class;
ns.getCounter = getCounter;
})();
반응형