0

我在网页上看到一个 java 脚本函数,它在函数顶部使用 with(),其余的函数实现在 with() 语句中执行。我把功能代码放在下面供参考。

function calculate()
{
     with (document.loan)
    {
      var loan = parseFloat(loan_amount.value);
      //function implementation goes here
    }
}

表格在带有贷款名称的页面中定义如下。

<form name="loan" id="loan-form">
   <input type="text" id="loan_amount"/>
  // remaining form elements here
</form>

这个“with”语句在做什么,它的作用域是什么?

4

1 回答 1

2

JavaScript 的with声明旨在为编写对对象的重复访问提供简写。

所以不要写

myObj.obj2.obj3.bing = true;
myObj.obj2.obj3.bang = true;

你可以写

with (myObj.obj2.obj3) {
    bing = true;
    bang = true;
}
于 2013-05-07T01:54:42.537 回答