if (!A) var A = {}; if (!A.B) A.B = {}; // careful here, var is not present here, only in the first one A.B.Test = function () { // ctor alert('constructor'); var _private_var = 'private var'; private_method = function () { alert('private method'); } alert('or can even write ctor anywhere'); return { public_property: "public_property", public_method: function () { alert("public_method"); } }; }; A.B.Test.public_static_property = 'public static property'; A.B.Test.public_static_method = function () { alert('public static method'); }; var t = A.B.Test(); alert(t.public_property); t.public_method(); if (typeof (t._private_var) != 'undefined') alert('can access private var, test failed'); else alert('cannot access private var, test passed'); try { t.private_method(); alert('can access private method, test failed'); } catch (e) { alert('cannot access private method, test passed'); } alert(A.B.Test.public_static_property); A.B.Test.public_static_method();
You can find more information on www.prabir.me