js with语句

来源:互联网 发布:孕期食谱软件 编辑:程序博客网 时间:2024/06/08 15:22

with语句的目的主要是为了简化多次编写同一个对象工作,如下面例子所示:

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

上面几行代码都包含location对象。使用with可以省去location重复编写:


with(location){
var qs=search.substring(1);
var hostName=hostname;
var url=href;
}

严格模式下不允许使用with,否则视为语法错误

原创粉丝点击