[·Òë]High Performance JavaScript(008)
Nested Members ǶÌ׳ÉÔ±
Since object members may contain other members, it's not uncommon to see patterns such as window.location.href in JavaScript code. These nested members cause the JavaScript engine to go through the object member resolution process each time a dot is encountered. Figure 2-12 shows the relationship between object member depth and time to access.
ÓÉÓÚ¶ÔÏó³ÉÔ±¿ÉÄܰüº¬ÆäËü³ÉÔ±£¬ÀýÈ粻̫³£¼ûµÄд·¨window.location.hrefÕâÖÖģʽ¡£Ã¿Óöµ½Ò»¸öµãºÅ£¬JavaScriptÒýÇæ¾ÍÒªÔÚ¶ÔÏó³ÉÔ±ÉÏÖ´ÐÐÒ»´Î½âÎö¹ý³Ì¡£Í¼2-12ÏÔʾ³ö¶ÔÏó³ÉÔ±Éî¶ÈÓë·ÃÎÊʱ¼äµÄ¹ØÏµ¡£
Figure 2-12. Access time related to property depth
ͼ2-12 ·ÃÎÊʱ¼äÓëÊôÐÔÉî¶ÈµÄ¹ØÏµ
It should come as no surprise, then, that the deeper the nested member, the slower the data is accessed. Evaluating location.href is always faster than window.location.href, which is faster than window.location.href.toString(). If these properties aren't on the object instances, then member resolution will take longer as the prototype chain is searched at each point.
½á¹û²¢²»Ææ¹Ö£¬³ÉԱǶÌ×Ô½É·ÃÎÊËÙ¶ÈÔ½Âý¡£location.href×ÜÊÇ¿ìÓÚwindow.location.href£¬¶øºóÕßÒ²Òª±Èwindow.location.href.toString()¸ü¿ì¡£Èç¹ûÕâЩÊôÐÔ²»ÊǶÔÏóµÄʵÀýÊôÐÔ£¬ÄÇô³ÉÔ±½âÎö»¹ÒªÔÚÿ¸öµãÉÏËÑË÷ÔÐÎÁ´£¬Õ⽫ÐèÒª¸ü³¤Ê±¼ä¡£
Caching Object Member Values »º´æ¶ÔÏó³ÉÔ±µÄÖµ
With all of the performance issues related to object members, it's easy to believe that they should be avoided whenever possible. To be more accurate, you should be careful to use object member only when necessary. For instance, there's no reason to read the value of an object member more than once in a single function:
ÓÉÓÚËùÓÐÕâЩÐÔÄÜÎÊÌâÓë¶ÔÏó³ÉÔ±Óйأ¬ËùÒÔÈç¹û¿ÉÄܵϰÇë±ÜÃâʹÓÃËüÃÇ¡£¸üÈ·ÇеØËµ£¬ÄãÓ¦µ±Ð¡Ðĵأ¬Ö»ÔÚ±ØÒªÇé¿öÏÂʹÓöÔÏó³ÉÔ±¡£ÀýÈ磬ûÓÐÀíÓÉÔÚÒ»¸öº¯ÊýÖжà´Î¶Áȡͬһ¸ö¶ÔÏó³ÉÔ±µÄÖµ£º
function hasEitherClass(element, className1, className2){
return element.c
Ïà¹ØÎĵµ£º
ZT:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
ÉÏÒ»´ÎµÄÎÄÕ£¬Ö÷Òª½éÉÜÁËÈçºÎ"·â×°"Êý¾ÝºÍ·½·¨£¬´ÓÔÐͶÔÏóÉú³ÉʵÀý¡£
½ñÌìÒª½éÉܵÄÊÇ£¬¶à¸öÔÐͶÔÏóÖ®¼äÈçºÎ"¼Ì³Ð"¡£
±ÈÈ磬ÏÖÔÚÓÐÒ»¸ö"¶¯Îï"¶ÔÏó£¬
¡¡¡¡function Animal(){
¡¡¡¡¡¡¡¡this.species = "¶¯Îï";
¡¡¡¡}
»¹ÓÐ ......
×î½ü±àдJavascript´úÂë¡£Æð³õû¹ÜÄÇô¶à¡£Ò»Õó¿ñд¡£´úÂëдµÃ²î²»¶àÁË¡£½á¹ûÉϰÙKÎļþ¼¸Ê®¸ö¡£µ±È» û°ì·¨ÐèҪѹËõÁË¡£ÎªÁËËÙ¶È¡£
ÕÒѹËõ¹¤¾ß¡£ÅªÁËÏ¡£½á¹û´íÎóÒ»´ó¶Ñ¡£×îºó²Å·¢ÏÖÊÇ×Ô¼ºÐ´µÄ´úÂë²»¹æ·¶µ¼Öµġ£¼ì²éÁ˰ëÌìÐÞÕýÁ˼¸Ê®¸öµØ·½¡£ÖÕÓÚÄÜѹËõÁË¡£
ÏÂÃæ×ܽáÏÂÐèҪעÒâµÄµØ·½
1¡¢¶ÔÏó½áβ function½áβ ×î ......
phpÖеÄevalºÍJavaScriptÖеÄevalÇø±ð£º
Definition and Usage
¶¨ÒåºÍÓ÷¨
The eval() function evaluates a
string as PHP code.
eval()º¯ÊýµÄ×÷ÓÃÊÇ£º·µ»ØÓëPHP´úÂëÏà¶ÔÓ¦µÄ×Ö·û´®¡£
The string
must be valid PHP code and must end with semicolon.
Õâ¸ö×Ö·û´®±ØÐëÊÇÓÐЧµÄPHP´úÂ룬
²¢ÇÒ±ØÐëÒԷֺŽáÊø¡ ......
1. document.formName.item("itemName") ÎÊÌâ
˵Ã÷:IEÏÂ,¿ÉÒÔʹÓÃdocument.formName.item("itemName")»òdocument.formName.elements["elementName"];
FirefoxÏÂ,Ö»ÄÜʹÓÃdocument.formName.elements["elementName"].
½â¾ö·½·¨:ͳһʹÓÃdocument.formName.elements["elementName"].
2.¼¯ºÏÀà¶ÔÏóÎÊÌâ
˵Ã÷:IEÏÂ,¿É ......