JavaScript学习-With语句

with 语句
    with 语句通常用来缩短特定情形下必须写的代码量。当程序多次使用某个对象的属性或方法,则我们可以使用With,在With内调用该对象,而不必在调用前加上对象.对象方法或属性。
格式:
with(对象名称)
{
    执行语句块
}

实例:

   <% -- JavaScript学习 -- %>
    
< script  type ="text/javascript"  language ="JavaScript" >  
        
function  DemoDate()
        {   
            
var  sStr;
            
var  dDate = new  Date();  
            
with (dDate)
            {
                
// 原写法应为dDate.getYear(),此处使用With后缩写为getYear()
                sStr = getYear() + " " ;
                sStr
+= getMonth() + " " ;
                sStr
+= getDate() + " " ;
                sStr
+= getHours() + " " ;
                sStr
+= getMinutes() + " " ;
                sStr
+= getSeconds() + " " ;  
            }         
            
return  sStr;
        }  

        
        
function  PersionShow()
        {
            
// 调用函数
            alert( " 日期为: " + DemoDate());           
        }
    
</ script >


            
<% -- 测试使用的按钮 -- %>
            
< input  id ="Button1"  type ="button"  runat ="server"  value ="测试"  onclick ="PersionShow();"   />
 

你可能感兴趣的:(JavaScript)