javascript中的with語句

with語句的做用是將代碼的做用域設置到一個特色的對象中。express

with (expression) statement;ide

定義with語句的目的只要是爲了簡化屢次編寫同一個對象的工做。url

 

  
  
           
  
  
  1. var qs = location.search.substring(1); 
  2. var hostName = location.hostname; 
  3. var url = location.href; 

 

上面幾行代碼都包含location對象。若是使用with語句,能夠把上面的代碼改爲以下所示:spa

 

  
  
           
  
  
  1. with(location) { 
  2.    var qs = location.search.substring(1);  
  3.     var hostName = location.hostname;  
  4.     var url = location.href;  
  5.  } 
相關文章
相關標籤/搜索