gpt4 book ai didi

javascript - javascript 函数中 with() 的用途

转载 作者:行者123 更新时间:2023-12-02 18:45:35 25 4
gpt4 key购买 nike

我在网页上看到一个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”语句在做什么以及它的范围是什么?

最佳答案

JavaScript 的 with 语句旨在提供一种编写对对象的重复访问的简写方式。

所以不要写

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

你可以写

with (myObj.obj2.obj3) {
bing = true;
bang = true;
}

关于javascript - javascript 函数中 with() 的用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com