ã¾ãã以ä¸ã®ãã㪠package é¢æ°ãä½ãã
function package(name) { package.__hash__ = package.__hash__ || {}; if (!package.__hash__.hasOwnProperty(name)) { package.__hash__[name] = { def: function(name, value) { return this[name] = value; } } } return package.__hash__[name] }
ã§ããããªæãã§ä½¿ã
// ãã㯠hoge ã®åå空é with (package('hoge')) { def('var0', 1); // å¤æ°ã¯ def é¢æ°ã§å®ç¾© def('var2', 2); alert(var0); // 1 alert(var2); // 2 } // ãã㯠fuga ã®åå空é with (package('fuga')) { def('var1', 3); def('var2', 4); alert(var1); // 3 alert(var2); // 4 } // ãã㯠hoge ã®åå空é with (package('hoge')) { alert(var0); // 1 alert(var2); // 2 } // ãã㯠hoge 㨠fuga ã®åå空éï¼ãã¨ã« with ããæ¹ãåªå ããï¼ with (package('hoge')) with(package('fuga')) { alert(var0); // 1 alert(var1); // 3 // fuga ã®åå空éãåªå ãã alert(var2); // 4 }
ãã®æ¹æ³ã使ãã¨
ã°ãã¼ãã«å¤æ°*1ãä¸å使ããã« JS ãæ¸ãã¾ãï¼
ã¡ãã£ã¨è§£èª¬
package é¢æ°ã¯ããã def ã¡ã½ãããæã£ããªãã¸ã§ã¯ããè¿ãã¦ãã ã
def ã¡ã½ããã¯èªåèªèº«ï¼ãªãã¸ã§ã¯ãï¼ã«ããããã£ã追å ããã
with æã¯ã¹ã³ã¼ããã§ã¼ã³ã«ããªãã¸ã§ã¯ãã追å ããã
ã¤ã¾ãã def ã¡ã½ããã§è¿½å ãããããããã£ãå¤æ°ã¨ãã¦è¦ããããã«ãªãã
ã£ã¦æãã§åä½ãã¦ã¾ãï¼ï¼
ã¾ã¨ã
with ã使ãã¨é
ããã©
éããæ°ã«ããªãã¨ãã«ä½¿ããï¼
便å©ï¼
*1:package é¢æ°ä»¥å¤ã®