Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[·­Òë]High Performance JavaScript(013)

Conditionals  Ìõ¼þ±í´ïʽ
    Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different browsers have implemented different flow control optimizations, it is not always clear which technique to use.
    ÓëÑ­»·ÏàËÆ£¬Ìõ¼þ±í´ïʽ¾ö¶¨JavaScriptÔËÐÐÁ÷µÄ×ßÏò¡£ÆäËûÓïÑÔʹÓÃif-else»òÕßswitch±í´ïʽµÄ´«Í³¹ÛµãÒ²ÊÊÓÃÓÚJavaScript¡£ÓÉÓÚ²»Í¬µÄä¯ÀÀÆ÷Õë¶ÔÁ÷³Ì¿ØÖƽøÐÐÁ˲»Í¬µÄÓÅ»¯£¬Ê¹ÓÃÄÄÖÖ¼¼Êõ²¢²»×ÜÊǺÜÇå³þ¡£
if-else Versus switch  if-elseÓëswitch±È½Ï
    The prevailing theory on using if-else versus switch is based on the number of conditions being tested: the larger the number of conditions, the more inclined you are to use a switch instead of if-else. This typically comes down to which code is easier to read. The argument is that if-else is easier to read when there are fewer conditions and switch is easier to read when the number of conditions is large. Consider the following:
    ʹÓÃif-else»òÕßswitchµÄÁ÷ÐÐÀíÂÛÊÇ»ùÓÚ²âÊÔÌõ¼þµÄÊýÁ¿£ºÌõ¼þÊýÁ¿½Ï´ó£¬ÇãÏòÓÚʹÓÃswitch¶ø²»ÊÇif-else¡£Õâͨ³£¹é½áµ½´úÂëµÄÒ׶ÁÐÔ¡£ÕâÖÖ¹ÛµãÈÏΪ£¬Èç¹ûÌõ¼þ½ÏÉÙʱ£¬if-elseÈÝÒ×ÔĶÁ£¬¶øÌõ¼þ½Ï¶àʱswitch¸üÈÝÒ×ÔĶÁ¡£¿¼ÂÇÏÂÃ漸µã£º
if (found){
  //do something
} else {
  //do something else
}
switch(found){
  case true:
    //do something
    break;
  default:
    //do something else
}
    Though both pieces of code perform the same task, many would argue that the if-else statement is much easier to read than the switch. Increasing the number of conditions, however, usually reverses that opinion:
    ËäÈ»Á½¸ö´úÂë¿éʵÏÖͬÑùÈÎÎñ£¬ºÜ¶àÈË»áÈÏΪif-else±í´ïʽ±Èwitch±í´ïʽ¸üÈÝÒ×ÔĶÁ¡£Èç¹ûÔö¼ÓÌõ¼þÌåµÄÊýÁ¿£¬Í¨³£»áŤתÕâÖֹ۵㣺
if (color == "red"){
  //do


Ïà¹ØÎĵµ£º

×Ô¼ºÐ´µÄjavascriptÎå×ÓÆå

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>five-in-a-raw</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <style>
  ......

javascript¼Ì³Ð·½Ê½Ö®Èý

3¡¢×éºÏ¹¹Ô캯Êý/Ô­ÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔ­ÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......

javaScript¹Ø±Õä¯ÀÀÆ÷ (²»µ¯³öÌáʾ¿ò)

£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......

Javascript±Õ°ü(Closure)

ClosureÖÐÎÄ·­ÒëΪ±Õ°ü.×ÖÃæÉÏÀ´Àí½â¾ÍÊÇ"·â±ÕµÄ°ü".(ÕâÊÇÒ»¾ä·Ï»°)
±Õ°üÊÇʲô?
ÊéÃæ½âÊÍΪ:
Ëùν“±Õ°ü”£¬Ö¸µÄÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÎÒÈÏΪ±Õ°ü¾ÍÊÇÄܹ»¶Á/дº¯ÊýÄÚ²¿µÄijЩ±äÁ¿µÄ×Óº¯Êý,²¢½«ÕâЩ±äÁ¿±£´æ ......

JavaScript»ù´¡ÖªÊ¶3

Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏ󼶱äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ