[·Òë]High Performance JavaScript(005)
µÚ¶þÕ Data Access Êý¾Ý·ÃÎÊ
One of the classic computer science problems is determining where data should be stored for optimal reading and writing. Where data is stored is related to how quickly it can be retrieved during code execution. This problem in JavaScript is somewhat simplified because of the small number of options for data storage. Similar to other languages, though, where data is stored can greatly affect how quickly it can be accessed later. There are four basic places from which data can be accessed in JavaScript:
¾µä¼ÆËã»ú¿ÆÑ§µÄÒ»¸öÎÊÌâÊÇÈ·¶¨Êý¾ÝÓ¦µ±´æ·ÅÔÚʲôµØ·½£¬ÒÔʵÏÖ×î¼ÑµÄ¶ÁдЧÂÊ¡£Êý¾Ý´æ´¢ÔÚÄÄÀ¹ØÏµµ½´úÂëÔËÐÐÆÚ¼äÊý¾Ý±»¼ìË÷µ½µÄËÙ¶È¡£ÔÚJavaScriptÖУ¬´ËÎÊÌâÏà¶Ô¼òµ¥£¬ÒòΪÊý¾Ý´æ´¢Ö»ÓÐÉÙÁ¿·½Ê½¿É¹©Ñ¡Ôñ¡£ÕýÈçÆäËûÓïÑÔÄÇÑù£¬Êý¾Ý´æ´¢Î»ÖùØÏµµ½·ÃÎÊËÙ¶È¡£ÔÚJavaScriptÖÐÓÐËÄÖÖ»ù±¾µÄÊý¾Ý·ÃÎÊλÖãº
Literal values Ö±½ÓÁ¿
Any value that represents just itself and isn't stored in a particular location. JavaScript can represent strings, numbers, Booleans, objects, arrays, functions, regular expressions, and the special values null and undefined as literals.
Ö±½ÓÁ¿½ö½ö´ú±í×Ô¼º£¬¶ø²»´æ´¢ÓÚÌØ¶¨Î»ÖᣠJavaScriptµÄÖ±½ÓÁ¿°üÀ¨£º×Ö·û´®£¬Êý×Ö£¬²¼¶ûÖµ£¬¶ÔÏó£¬Êý×飬º¯Êý£¬ÕýÔò±í´ïʽ£¬¾ßÓÐÌØÊâÒâÒåµÄ¿ÕÖµ£¬ÒÔ¼°Î´¶¨Òå¡£
Variables ±äÁ¿
Any developer-defined location for storing data created by using the var keyword.
¿ª·¢ÈËԱʹÓÃvar¹Ø¼ü×Ö´´½¨ÓÃÓÚ´æ´¢Êý¾ÝÖµ¡£
Array items Êý×éÏî
A numerically indexed location within a JavaScript Array object.
¾ßÓÐÊý×ÖË÷Òý£¬´æ´¢Ò»¸öJavaScriptÊý×é¶ÔÏó¡£
Object members ¶ÔÏó³ÉÔ±
A string-indexed location within a JavaScript object.
¾ßÓÐ×Ö·û´®Ë÷Òý£¬´æ´¢Ò»¸öJavaScript¶ÔÏó¡£
Each of these data storage locations has a particular cost associated with reading and writing operations involving the data. In most cases, the performance difference between accessing information from a literal value versus a local variable is trivial. Accessing info
Ïà¹ØÎĵµ£º
/**
* JavaScript ÊÇÃæÏò¶ÔÏóµÄÓïÑÔ£¬µ«ÊÇËûµÄÃæÏò¶ÔÏó²»ÊÇ»ùÓÚÀàµÄ£¬ÊÇ»ùÓÚÔÐ͵Ä;
* µ«ÊÇËûµÄÒ»Ð©ÌØÐÔ(º¯ÊýÊÇÊý¾Ý)£¬Ê¹µÃËü¿ÉÒÔÄ£Äâ»ùÓÚÀàµÄÃæÏò¶ÔÏ󣬵«ÊÇ JavaScript ²¢²»Ö§³ÖºÍ Java Ò»ÑùµÄÀ࣬
* Òò´Ë JavaScript ÖеÄ"Àà"¿ÉÒÔ³Æ×÷"αÀà"
*/
//
/**
* ¹¹Ô캯Êý: js ÖÐÓÃÀ´ºÍ new ÔËËã·ûÒ»ÆðʹÓõĺ¯Êý³Æ× ......
¿´ÊéµÄʱºòÓöµ½ÕâÑùÒ»¸öÎÊÌ⣬³ÌÐò´úÂëÈçÏÂ
var ob = function(){
var obj = this;
function fn1(){
alert( obj === window );//false
alert( this === window );//ture
}
this.fn2 = function() {
fn1();
}
}
µ±Ê±ºÜ²»Ã÷°×fn1ÀïÃæµÚ¶þ¸öalertµÄ½á¹û£¬Îª ......
´ÓÕâÆªÆð£¬»áÓÉdzµ½ÉîµÄ·ÖÎöjs OO֮дÀ෽ʽ£¬´ó¸Å»áÓÐ5-8ƪ¡£ºóÃæÂ½Ðø»á·ÖÎöÁ÷Ðп⣨¿ò¼Ü£©µÄдÀ෽ʽ¡£ÎªÁËÌÖÂ۵ĵ¥Ò»ÐÔ£¬Ôݲ»¿¼ÂÇÀàµÄ¼Ì³Ð£¬(˽ÓÐ,Êܱ£»¤)ÊôÐÔ»ò·½·¨¡£
EMCA262¹æ·¶ÖÐûÓÐÀà(class)µÄ¸ÅÄjsµÄnewÖ»ÊÇÈÃËû¿´ÆðÀ´¸üÏñc++,javaÒ»µã¡£ÕâÀï˵µÄдÀֻ࣬ÊÇÊéдjs´úÂë·ç¸ñ¶øÒÑ¡£
1¡¢¹¹Ô캯Êý·½Ê½
/**
* P ......
Grouping Scripts ³É×é½Å±¾
Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time ......
Dynamic Script Elements ¶¯Ì¬½Å±¾ÔªËØ
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrie ......