[·Òë]High Performance JavaScript(025)
µÚ°ËÕ Programming Practices ±à³Ìʵ¼ù
Every programming language has pain points and inefficient patterns that develop over time. The appearance of these traits occurs as people migrate to the language and start pushing its boundaries. Since 2005, when the term "Ajax" emerged, web developers have pushed JavaScript and the browser further than it was ever pushed before. As a result, some very specific patterns emerged, both as best practices and as suboptimal ones. These patterns arise because of the very nature of JavaScript on the Web.
ÿÖÖ±à³ÌÓïÑÔ¶¼ÓÐÍ´µã£¬¶øÇÒµÍÐ§Ä£Ê½Ëæ×Åʱ¼äµÄÍÆÒÆ²»¶Ï·¢Õ¹¡£ÆäÔÒòÔÚÓÚ£¬Ô½À´Ô½¶àµÄÈËÃÇ¿ªÊ¼Ê¹ÓÃÕâÖÖÓïÑÔ£¬²»¶ÏÀ©ÖÖËüµÄ±ß½ç¡£×Ô2005ÄêÒÔÀ´£¬µ±ÊõÓï“Ajax”³öÏÖʱ£¬ÍøÒ³¿ª·¢Õß¶ÔJavaScriptºÍä¯ÀÀÆ÷µÄÍÆ¶¯×÷ÓÃÔ¶³¬¹ýÒÔÍù¡£Æä½á¹ûÊdzöÏÖÁËһЩ·Ç³£¾ßÌåµÄģʽ£¬¼´ÓÐÓÅÐãµÄ×ö·¨Ò²ÓÐÔã¸âµÄ×ö·¨¡£ÕâЩģʽµÄ³öÏÖ£¬ÊÇÒòÎªÍøÂçÉÏJavaScriptµÄÐÔÖʾö¶¨µÄ¡£
Avoid Double Evaluation ±ÜÃâ¶þ´ÎÆÀ¹À
JavaScript, like many scripting languages, allows you to take a string containing code and execute it from within running code. There are four standard ways to accomplish this: eval(), the Function() constructor, setTimeout(), and setInterval(). Each of these functions allows you to pass in a string of JavaScript code and have it executed. Some examples:
JavaScriptÓëÐí¶à½Å±¾ÓïÑÔÒ»Ñù£¬ÔÊÐíÄãÔÚ³ÌÐòÖлñȡһ¸ö°üº¬´úÂëµÄ×Ö·û´®È»ºóÔËÐÐËü¡£ÓÐËÄÖÖ±ê×¼·½·¨¿ÉÒÔʵÏÖ£ºeval()£¬Function()¹¹ÔìÆ÷£¬setTimeout()ºÍsetInterval()¡£Ã¿¸öº¯ÊýÔÊÐíÄã´«ÈëÒ»´®JavaScript´úÂ룬ȻºóÔËÐÐËü¡£ÀýÈ磺
var num1 = 5,
num2 = 6,
//eval() evaluating a string of code
result = eval("num1 + num2"),
//Function() evaluating strings of code
sum = new Function("arg1", "arg2", "return arg1 + arg2");
//setTimeout() evaluating a string of code
setTimeout("sum = num1 + num2", 100);
//setInterval() evaluating a string of code
setInterval("sum = num1 + num2", 100);
Whenever you're evaluating Jav
Ïà¹ØÎĵµ£º
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
......
µÚÎåÕ Strings and Regular Expressions ×Ö·û´®ºÍÕýÔò±í´ïʽ
Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and ......
µÚÁùÕ Responsive Interfaces ÏìÓ¦½Ó¿Ú
There's nothing more frustrating than clicking something on a web page and having nothing happen. This problem goes back to the origin of transactional web applications and resulted in the now-ubiquitous "please click only once" m ......
Yielding with Timers Óö¨Ê±Æ÷Èóöʱ¼äƬ
Despite your best efforts, there will be times when a JavaScript task cannot be completed in 100 milliseconds or less because of its complexity. In these cases, it's ideal to yield control of the UI thread so that UI updates may occur ......