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

posted @ 2016-09-30 17:18  TBHacker  阅读(2014)  评论(0编辑  收藏  举报