JS¼¼Êõ

±©Á¦·ÖÎöbackbone.js(2)

×ÖºÅ+ ×÷Õߣºsuperjs À´Ô´£ºcnblog 2015-06-06 16:35 ÎÒÒªÆÀÂÛ( )

¼ÌÐøÉÏÒ»½ÚÄÚÈÝ£¬´ø×ÅÒÉÎʺͼòµ¥µÄDomeÀ´·ÖÎöbackbone.js¡£ Õâ´ÎÎҰѼòµ¥Dome Copyµ½±¾µØ£¬ÒýÈëbackbone.js¿â¡£(ÕâÀïÎҾͲ»ÒýÈëÆäËû¿âÁË£¬Âé·³£¬ËùÒÔÒª¼òµ¥ÐÞ¸Äһϣ¡) 1 body 2 script src =backbone.js / script 3 script 4 ( function (doc){ 5 6 var Lis

¼ÌÐøÉÏÒ»½ÚÄÚÈÝ£¬´ø×ÅÒÉÎʺͼòµ¥µÄ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/

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • SSHÍøÉÏÉ̳Ç---ÐèÇó·ÖÎö+±í¹Øϵ·ÖÎö - ¶¡¹ú»ª ÀÈ·»Ê¦·¶Ñ§ÔºÐÅÏ¢¼¼ÊõÌá¸ß°à µÚÊ®ÆÚ - ²©¿ÍƵµÀ - CSDN.N

    SSHÍøÉÏÉ̳Ç---ÐèÇó·ÖÎö+±í¹Øϵ·ÖÎö - ¶¡¹ú»ª ÀÈ·»Ê¦·¶Ñ§ÔºÐÅÏ¢¼¼Êõ

    2015-12-14 17:36

  • ²éÕÒÕ»µÄÔö³¤·½ÏòµÄ·ÖÎö¼°C´úÂëʵÏÖ - ÖÜÕ×ÐܵÄרÀ¸ - ²©¿ÍƵµÀ - CSDN.NET ÖÜÕ×ÐܵÄרÀ¸ 2015Äê¶È¡°²©

    ²éÕÒÕ»µÄÔö³¤·½ÏòµÄ·ÖÎö¼°C´úÂëʵÏÖ - ÖÜÕ×ÐܵÄרÀ¸ - ²©¿ÍƵµÀ - CS

    2015-12-14 15:15

  • È«Çò×îµÍ¹¦ºÄÀ¶ÑÀµ¥Ð¾Æ¬£¨DA14580£©ÏµÍ³¼Ü¹¹ºÍÓ¦Óÿª·¢¿ò¼Ü·ÖÎö - ǶÈëʽÆó¶ìȦ - ²©¿ÍƵµÀ - CSDN.NET

    È«Çò×îµÍ¹¦ºÄÀ¶ÑÀµ¥Ð¾Æ¬£¨DA14580£©ÏµÍ³¼Ü¹¹ºÍÓ¦Óÿª·¢¿ò¼Ü·ÖÎö - Ƕ

    2015-12-13 11:31

  • ¹ØÓÚËÑË÷ÒýÇæÒ³Ãæ·ÖÎöÖÐµÄ javascript ´¦ÀíµÄ2¸ö˼·_javascript½Ì³Ì½Ì³Ì

    ¹ØÓÚËÑË÷ÒýÇæÒ³Ãæ·ÖÎöÖÐµÄ javascript ´¦ÀíµÄ2¸ö˼·_javascript½Ì³Ì

    2015-10-04 10:02

ÍøÓѵãÆÀ
ß