[·Òë]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
Ïà¹ØÎĵµ£º
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
function total(){
var i=0;
for(j=1;j<=20;j++)
{
var step="step"+j;
if(document.getElementById(step)){
if(document.getElementById(step).checked==true)
{
i=i+parseInt(document.getElementById(step).value);
}
}
}
document.getElementById("total").innerHTML = i;
}
function Resetvalue(){
......
Repaints and Reflows ÖØ»æºÍÖØÅŰæ
Once the browser has downloaded all the components of a page—HTML markup, JavaScript, CSS, images—it parses through the files and creates two internal data structures:
µ±ä¯ÀÀÆ÷ÏÂÔØÍêËùÓÐÒ³ÃæHTML±ê¼Ç£¬JavaScri ......
Splitting Up Tasks ·Ö½âÈÎÎñ
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller ......
µÚÆßÕ Ajax Òì²½JavaScriptºÍXML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client ......