JavaScript 核心语句 - with 对象操作语句

在编写JavaScript 脚本过程中,经常需引用同一对象的多个属性或方法,正常的对象属性或方法的引用途径能达到既定的目的,但代码显得尤为复杂。

JavaScript 脚本语言提供with操作语句来简化对象属性和方法的引用过程,其语法结构如下:with (objct) { statements; }例如下列连续引用document 对象的write()方法的语句:document.write("Welcome to China"); document.write("Welcome to Beijing"); document.write("Welcome to Shanghai");可以使用with 语句简化为:with(document) { write("Welcome to China"); write("Welcome to Beijing"); write("Welcome to Shanghai"); }在脚本代码中适当使用with 语句可使脚本代码简明易懂,避免不必要的重复输入。

若脚本代码中涉及到多个对象,不推荐使用with 语句,避免造成属性或方法引用的混乱。