JS中with函数的作用

JavaScript010

JS中with函数的作用,第1张

with 语句为语句设定默认对象。 with (object)

statements 参数 object新的默认对象。statements一个或多个语句,object 是该语句的默认对象。说明</B>with 语句通常用来缩短特定情形下必须写的代码量。在下面的例子中,请注意 Math 的重复使用: x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10) y = Math.tan(14 * Math.E) 当使用 with 语句时,代码变得更短且更易读: with (Math){ x = cos(3 * PI) + sin (LN10)y = tan(14 * E)}

就是用document这个对象的意思,

简写代码,不用重复写document

如:

with(document)

{.title='aaa'

}

相当于:

document.title='aaa'