ãã ããHabrahabr ã®æ€çŽ¢çµæã«ã¯ãResigããšããåèªããŸã£ãã衚瀺ãããªããããéå»4幎éã«ããã£ãŠãã®äŸ¿å©ãªããã°æçš¿ã翻蚳ããïŒãŸãã¯å°ãªããšãåèªããïŒããšã誰ãæ°ã«ããã«èããªããã°ãªããŸããã ãRezigã®ããã°æçš¿ãç§ãèªåã®äž»ãªæå³ãæããåã«åèªããããšïŒRezigã«ãã£ãŠææ¡ãããåé¡ã解決ããããã«Rezigã«ãã£ãŠææ¡ãããæ¹æ³ãåºãŸã£ãŠããªãçç±ãèãåºãããšã ãããŠãç§ã¯èšãçŽããŸãã ïŒèªã¿çŽãèªäœã¯ãèªè ã«äœãè¿œå ããªãã£ããšããŠããèªè ã«ãšã£ãŠã¯ãã§ã«æçšã§ããè¿œå ããŸããïŒ
2007幎12æ6æ¥ã«ãRezigã¯javascriptã§ãnewãæäœã䜿çšããŠãªããžã§ã¯ããäœæããå Žåã«äœãèµ·ãããã調ã¹ãŸããïŒã¯ã©ã¹ãæã€èšèªã§ã¯ããã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ããšèšããŸãïŒã
function User(first, last){ this.name = first + " " + last; } var user = new User("John", "Resig");
Rezigã¯ãjavascriptã®åå¿è ã«ãšã£ãŠãé¢æ°ã³ãŒãå ã®ãthisãã®åºçŸããªããžã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãæã£ãŠããããšã瀺ããŠããããšã¯æããã§ã¯ãªãããšãæ£ããææããŸããã ïŒèªåã§æ¬åŒ§ãä»ããŸãïŒé¢æ°ãã©ã€ãã©ãªã®è žå ã«ããå Žåããã®ç¶æ³ãææžåããå¿ èŠããããŸã-ããã§ãªãå Žåãã©ã€ãã©ãªãŠãŒã¶ãŒã¯åå¿è ãšããã»ã©å€ãããŸããïŒèª°ããé¢æ°ã®æ¬äœã§ãœãŒã¹ã³ãŒããèªãããã§ã¯ãããŸãããå€ãã®å Žåãçž®å°ãããèªã¿åãäžèœãªåœ¢åŒã§äœ¿çšãããŸããïŒ
ãããã£ãŠãRezigã¯ãé ããæ©ãã
var name = "Resig"; var user = User("John", name); // «user» // : «name» é «Resig»! if ( name == "John Resig" ) { // !⊠}
ããã§ããResigã¯ããã«ææããã³ã³ã¹ãã©ã¯ã¿ãŒã®åŒã³åºãã¯äŸ¿å©ã§ãã ãããã¿ã€ãã®ç¶æ¿ïŒãããã¿ã€ããããªããžã§ã¯ãã®ããããã£ãååŸããïŒãã€ãŸãå®éã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããšã¯ãåçŽãªé¢æ°ãåŒã³åºããŠãªããžã§ã¯ããšåãããããã£ããæ§ç¯ããããããã¯ããã«é«éã«åäœãããšããå©ç¹ããããŸãã
// : function User(){} User.prototype = { /* ⊠⊠*/ }; // : function User(){ return { /* ⊠⊠*/ }; }
Rezigã¯ãäžæ¹ã§ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒãšããŠæ©èœãïŒã¯ã€ãã¯ãããã¿ã€ãã®ç¶æ¿ãæäŸããïŒãä»æ¹ã§ã¯
幞ããªããšã«ãResigã¯ç¶ããŸãããããããã¹ãŠã®åé¡ã¯ãé¢æ°æ¬äœã®æ¡ä»¶ä»ãè¡šèšã䜿çšããç°¡åãªè§£æ±ºçã«åœ¹ç«ã¡ãŸãã
function User(first, last){ if ( this instanceof User ) { // : this.name = first + " " + last; } else { // : return new User(first, last); } }
ããã§ã®instanceofæŒç®åã¯ã
function test(){ alert( this instanceof test ); } test(); // alert( false ); new test(); // alert( true );
ãã®ãœãªã¥ãŒã·ã§ã³ãèŠã€ããŠããããæ©èœããŠããããšã確èªããŠãããRezigæ°ã¯æ¬¡ã®ããã«è¿°ã¹ãŸããã次ã«ããã®ãœãªã¥ãŒã·ã§ã³ããã¯ã©ã¹ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒãäœæããäžè¬çãªæ¹æ³ã§ã©ããããŸãã ãã®ç®çã®ããã«ãJohn Rezigã¯æ¬¡ã®ç¡æã®ã³ãŒããã¬ã€ã¢ãŠãããŸããã
// makeClass - By John Resig (MIT Licensed) function makeClass(){ return function(args){ if ( this instanceof arguments.callee ) { if ( typeof this.init == "function" ) this.init.apply( this, args.callee ? args : arguments ); } else return new arguments.callee( arguments ); }; }
ãã®ã³ãŒãã䜿çšããåã®äŸã¯ãåã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®æ¬äœããããã¿ã€ãã®ã init ãã¡ãœããã®æ¬äœã«ãªãããã«æžãçŽãå¿ èŠããããŸãã
var User = makeClass(); User.prototype.init = function(first, last){ this.name = first + " " + last; }; var user = User("John", "Resig"); user.name // «John Resig»
ã makeClass ãJohn Rezigã®äœæ¥ã®è«çãååã«è©³çŽ°ã«èª¬æãããŠããŸãã ã makeClassïŒïŒ ãé¢æ°ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒã§ã¯ãªããã³ã³ã¹ãã©ã¯ã¿ãŒãäœæããŸã-ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ã
John Rezigã®ææ ®æ·±ãããã°ã®æžãçŽãã¯çµãããŸããã ä»ãç§ã¯åœŒèªèº«
圌ã®
ããŸããŸãªjavascriptãã©ã€ãã©ãªã®äœæè ã®ã³ãã¥ããã£ã«ããã
èªå·±åŒã³åºãã³ã³ã¹ãã©ã¯ã¿ãŒã«å¯Ÿããæµæã®ãã1ã€ã®çç±ã¯ã
ããã¯äœåºŠãèŠãŸããã
ç§ã¯ããã®ïŒ2011幎ïŒ5æã«azproductionã«ãã£ãŠã³ã³ãã€ã«ãããJavaScript FAQ㧠ã次ã®ããã«èšãããããšãèŠããŠããŸã ã
-æ°ãããã®ãéããŠãªããžã§ã¯ããäœæããããšã¯ãããè¯ãããã身è¿ã§ãã€ããªãã®ãŒçã§ãã ãã¶ã€ããŒã¯å€§æåã«ããå¿ èŠããããŸãã
-ç§ã¯èŠçŽã«åºã¥ããŠããããšã奜ã¿ãã³ã³ã¹ãã©ã¯ã¿ãŒå ã§ããããã§ãã¯ããŸãã-ç§ã¯æ°ããã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºããããããã£ãŠã°ããŒãã«ã«é£ãã§ããŸã-ããã¯ãæãè èªèº«ããæå³ããŸãã ãããŠã決ããŠç§ã¯newã§ãšã©ãŒã奚å±ããŸãã-äžéšã®äººã ã¯ãããã°ããŒãã«ãªãã®ã§ãããã©ããã確èªãããŠãŒã¶ãŒãnewãªãã§ã³ã³ã¹ãã©ã¯ã¿ãåŒã³åºããã³ã³ã¹ãã©ã¯ã¿å ã«ãªããžã§ã¯ããäœæããŠè¿ããŸã-ããã¯ãšã©ãŒãžã®å¥šå±ã§ãããã€ããªãã®ãŒçã«ééã£ãã¢ãããŒãã§ãã
ïŒåŒçšã®çµãããïŒ
ãŸããå°å³ã衚瀺ããçŸãããªãŒãã¬ããã©ã€ãã©ãªã®äœæè ã§ãããŠã©ãžããŒã«ã¢ã¬ãã©ã³ãã³ã®äºäŸãèŠããŠããŸãã ãã®ïŒ2011幎ïŒ8æã«ã圌ã¯Githubã§ã®å䜵ã®ãªã¯ãšã¹ããåãåããŸãããèè ã¯ãåãã¶ã€ããŒã®åé ã§ãã®ã³ãŒãã®ãããªãã®ã眮ãããšãææ¡ããŸããã
if ( !(this instanceof arguments.callee) ){ return new arguments.callee(arguments); }
ã¢ã¬ãã©ã³ãã³ã¯ããã«çããïŒ
-åå¿è ã®JSäœæè ãééããç¯ããªãããã«ããããšã¯éåžžã«äŸ¿å©ã§ããããŠãŒã¶ãŒã«ééã£ãŠããããšãäŒããã®ã§ã¯ãªãã誀ã£ãæ§æãèš±å¯ãããããªéãèšèªã®ã¢ã€ãã¢ã¯å¥œãã§ã¯ãããŸããã
-ãnewãããªããŠããªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ãäœæããã®ã§ã¯ãªããã new ErrorãïŒãã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒã®åã«æ°ããããŒã¯ãŒãã眮ãã®ãå¿ãããïŒã®ãããªããšãããæ¹ãè¯ãããã«æããŸãã
-ãããŠããã1ã€ãããŸããarguments.calleeã¯çŸåšæªæããããšèŠãªãããŠãããããã¯ã©ã¹åãæ瀺çã«èšè¿°ããæ¹ãå®å šã§ãã
ïŒåŒçšã®çµãããïŒ
ãªã¯ãšã¹ãã®äœæè ã¯ãè¡ã£
ã©ã®ãªãŒãã¬ãããŠãŒã¶ãŒãå°ãªããšãã ã¯ã€ãã¯ã¹ã¿ãŒãã¬ã€ã ããèªããã¯ããã®ã©ã€ãã©ãªã§å®çŸ©ãããŠããã°ããŒãã«ãªããžã§ã¯ããïŒæããã«ãç°¡æœã«ããããã«ïŒ
John Rezigã
function User(first, last){ if ( this instanceof User ) { // : this.name = first + " " + last; } else { // : return new User(first, last); } }
ãã ãããã¡ãããé¢æ°å šäœã«äœåãª
function User(first, last){ if (!(this instanceof User)) return new User(first, last); // this.name = first + " " + last; // ⊠⊠}
ãããŠã
ãã®ãããªè¯ãäŸã«åŸãã®ã¯ç°¡åã§æ¥œããã§ãã ãŸããã³ã³ã¹ãã©ã¯ã¿ãŒã³ã³ã¹ãã©ã¯ã¿ãŒãããç解ãããããªã£ãŠããŸã-javascriptãªããã£ãã€ã¶ãŒãããæ確ã«å«ã¿ãŸãã
人çã§åæ§ã®è¯å®çãªäŸãæåŸã«èŠããå Žåã¯ã
var Reader = exports.Reader = function (data) { if (!(this instanceof Reader)) return new Reader(data); this._data = data; this._offset = 0; }
çµè«ã¯ç°¡åã§ãããžã§ã³ã»ã¬ãžã°ã®äœåãç 究ãã圌ã®ã¢ããã€ã¹ã«åŸãã圌ã®æ瀺ã«åŸã£ãŠè¡åããŠãã ããã ããããè€éãã®ç¹å®ã®å¶éã«ã®ã¿ã
ä»å±ç©ã ããã«ããããããã2012幎7ææ«ãŸã§ã«ããŠã©ãžããŒã«