¼ÌÐøÉÏÒ»½ÚÄÚÈÝ£¬´ø×ÅÒÉÎʺͼòµ¥µÄDomeÀ´·ÖÎöbackbone.js¡£
¡¡¡¡Õâ´ÎÎҰѼòµ¥Dome Copyµ½±¾µØ£¬ÒýÈëbackbone.js¿â¡£(ÕâÀïÎҾͲ»ÒýÈëÆäËû¿âÁË£¬Âé·³£¬ËùÒÔÒª¼òµ¥ÐÞ¸Äһϣ¡)
1 <body> 2 <script src="backbone.js"></script> 3 <script> 4 (function(doc){ 5 6 var ListView = Backbone.View.extend({ 7 el: doc.getElementsByTagName('body'), 8 9 initialize: function(){ 10 _.bindAll(this, 'render'); 11 12 this.render(); 13 }, 14 15 render: function(){ 16 var ListUl = doc.createElement('ul'); 17 ListUl.innerHTML = '<li>hello world</li>'; 18 this.el.appendChild(ListUl); 19 } 20 }); 21 22 var listView = new ListView(); 23 })(document); 24 </script> 25 </body>
¡¡¡¡È»ºó´ò¿ªÍøÒ³£¬±¨´íÁË£¡£¡£¡´Ó´íÎó¿ªÊ¼×ÅÊÖ£¡£¡£¡
¡¡¡¡µã»÷backbone.js:224 ¶¨Î»±¨´íλÖÃ
¡¡¡¡¶ÏµãÁËһϣ¬ÕâÖÖд·¨£¬_ ÆäʵÊÇÒ»¸ö¶ÔÏó£¬eachÊôÓÚ_¶ÔÏóµÄÒ»¸ö·½·¨£¬ÎÒÃÇ´òÓ¡ÁËһϣ¬·¢ÏÖΪundefined£¬¼ÌÐøÍùÉÏÃæÅŲ顣
¡¡¡¡»á·¢ÏÖ_¶ÔÏóÊǺ¯ÊýµÄ´«²Î£¬ÒòΪû´«½øÀ´£¬ËùÒÔÊÇundefined£¬ËùÒÔÏÖÔÚÎÒÃǼÌÐøÕÒ£¬¿´ÄÄÀïÖ´ÐÐÕâ¸öº¯Êý£¡
¡¡¡¡ÔÚÕâ֮ǰÎÒÃÇÏÈÀ´¿´Ò»ÏÂbackboneµÄÍâ¿Ç¡£Ò»¸ö×ÔÖ´Ðк¯Êý£¬²ÎÊýroot¡¢factory£¬·Ö±ð¶ÔÓ¦ this ¡¢ func()¡£²»ÄÑ·¢ÏÖthisÖ¸ÏòµÄ¾ÍÊÇ window£¬È»¶øÎÒÃÇÒªÕÒµÄÊÇÔÚÄÄÀïÖ´ÐÐÁËfunc()£¬func()×÷Ϊ²ÎÊý¸³¸øÁËÐβÎfactory£¬Ò²¾ÍÊÇ˵ÎÒÃÇÒªÔÚÕâ¸ö×ÔÖ´Ðк¯ÊýÄÚÑ°ÕÒµ÷ÓÃfactioryµÄλÖ㬿´factioryµÄ4¸ö²ÎÊý´«¶ÔÁËûÓУ¿
1 (function (root, factory) { 2 ¡¡¡¡ 3 }(this, function () { 4 /***/ 5 }))
¡¡¡¡×ÔÖ´Ðк¯ÊýÄÚÊÇifÓï¾ä£¬ÎÒ¸øÿ¸öifÓï¾äµÄ¿ªÊ¼¼ÓÁ˶ϵã debugger£¬¿´¾ÍÒýÓÃÁËbackboneÊÇÖ´ÐÐÄĸö£¿½á¹ûÊÇÖ´ÐÐÁË ×îºóÒ»¸ö£¬È»ºóÍùÉÏ¿´Ò»ÏÂ2¸öÌõ¼þ£º
¡¡¡¡¡¡¡¡1.typeof define === 'function' && define.amd
¡¡¡¡¡¡¡¡2.typeof exports !== 'undefined'
¡¡¡¡Èç¹ûÄãϸÐĵĿ´×¢ÊͲ»ÄÑ·¢ÏÖdefine ºÍ exportsÊÇ2¸öÒÀÀµ¿â£¬Èç¹ûËüÃǶ¼ÓÐÒýÓã¬ÄÇô defineµÄÓÅÏȼ¶×î¸ß¡£¾ÍËã²»¿´×¢ÊÍ£¬Ï¸ÐÄ¿´£¬Äã»á·¢ÏÖÔÉú¸ù±¾Ã»Õâ2¸ö¶«Î÷£¬°Ù¶ÈËÑһϾÍÖªµÀÁË£¬Èç¹ûÄãÈç¹ûÄãÓùý¾Í¸ú²»ÓÃ˵ÁË¡£
¡¡¡¡ÎÒʲô¶¼Ã»ÒýÓÃ×ߵľÍÊÇÓÅÏȼ¶×îµÍµÄ¿©¡£
¡¡¡¡¼ÌÐø×ßÏÂÈ¥£¬»¹ÊǶϵ㡣
¡¡¡¡¡¡¡¡root === window
¡¡¡¡¡¡¡¡root._ === £¿£¿£¿ windowÏÂÃæûÓÐÕâ¸ö¶ÔÏó°¡£¡(Ôõô°ì£¬»ØÍ·¿´Ò»Ï±ðÈËdomeÒýÓõĿ⣬³ýÁËjQueryÖ®Í⻹ÓÐunderscore ºÍ json2)
¡¡¡¡¡¡¡¡ÔÚ¿´func()µÄÐβÎ$ ¶ÔÓ¦µÄÊÇÕâÑùÒ»´®±í´ïʽ (root.jQuery || root.Zepto || root.ender || root.$)£¬Ò²¾ÍÊÇ˵backbone±ØÐëÒªÓÐÒÀÀµ¿â£¬Õâ¾ä±í´ïʽµÄÒâ˼¾ÍÊÇ£¬window.jQueryÓеĻ°£¬¾ÍÒýÓÃwindow.jQuery£¬ÒÀ´ÎÀàÍÆ£¬È«²¿Ã»ÓоÍÊÇundefined£¬Ê²Ã´¿â¶¼²»ÒªÒýÓÃÖ±½ÓÖ´ÐÐÕâ¾ä±í´ïʽÊÔÊÔ¿´¡£
¡¡¡¡ËùÒÔ×ß×îºóÒ»Ìõ·£¬backboneËùÒÀÀµµÄ jQuery ºÍ undercore ÊDZز»¿ÉÉٵģ¬ÀÏÀÏʵʵ°ÑËüÃÇÒýÓýøÀ´°É
1 (function(root, factory) { 2 3 if (typeof define === 'function' && define.amd) { 4 debugger; 5 define(['underscore', 'jquery', 'exports'], function(_, $, exports) { 6 root.Backbone = factory(root, exports, _, $); 7 }); 8 9 } else if (typeof exports !== 'undefined') { 10 debugger; 11 var _ = require('underscore'); 12 factory(root, exports, _); 13 14 } else { 15 debugger; 16 root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$)); 17 } 18 19 }(this,function(root, Backbone, _, $){ 20 /**/ 21 })
¡¡¡¡Õñ·ÜÈËÐĵÄʱºòÀ´ÁË£¬¹ûÈ»ÅÜͨÁË£¡£¡£¡
¡¡¡¡
¡¡¡¡½â¾öÁËÎÊÌ⣬ÏÈÐÝϢһϣ¬ÔÚ¼ÌÐø·ÖÎöbackbone£¡
±¾ÎÄת×Ô£ºhttp://www.cnblogs.com/superjs/
¡¡