with 语句用于设置代码在特定对象中的作用域。
语法:
with (expression) statement
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10);
y = Math.tan(14 * Math.E);
with (Math) {
x = cos(3 * PI) + sin(LN10);
y = tan(14 * E);
}
像上面的代码,Math是对象,cos、sin、tan是它的值,如果没有with,就应该是Math.cos
本文介绍了JavaScript中的with语句,解释了其如何简化代码中的对象属性调用,通过示例展示了使用with语句可以避免重复书写对象名称,提高代码的可读性和简洁性。
1241






