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

原文地址:https://www.cnblogs.com/jiqing9006/p/5924376.html