Javascript中的with用法


1.看例子

<script language="javascript">  
function Lakers() {  
       this.name = "kobe bryant";  
       this.age = "28";  
       this.gender = "boy";  
}  
var people=new Lakers();  
with(people)  
{  
       var str = "姓名: " + name + "<br>";  
       str += "年齡:" + age + "<br>";  
       str += "性別:" + gender;  
       document.write(str);  
}  
</script>  

代碼執行效果如下:

姓名: kobe bryant
年齡:28
性別:boy

2.系統對象

     <script type="text/javascript">
    
        with(location){
            var hostName = hostname;    
            var url = href;
        }
        alert(hostName);
        alert(url);

    </script>

執行結果:

localhost
http://localhost/xxx.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM