2013-05-07 1 views
0

with() 문 안에서 함수()와 함께 사용되는 나머지 웹 페이지에서 자바 스크립트 함수를 볼 수 있습니다. 나는 아래의 함수 코드를 참조 용으로 두었다.javascript 함수에서 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"구문은 무엇이며 어떤 범위입니까?

+0

중복 가능성을 쓸 수 있습니다 (http://stackoverflow.com/questions/61552/is-there-legal-use-for-statement with javascript-with-statement) –

+0

유용한 정보 : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/with –

+0

이 질문은 : http://stackoverflow.com/q/61552/1846192 및이 답변 : http://stackoverflow.com/a/18528 3/1846192는 귀하에게 관심을 가질 것입니다. –

답변

2

JavaScript의 with 문은 객체에 대한 반복 액세스를 작성하기위한 속기를 제공하기위한 것입니다. 그래서 그 대신 쓰기

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

당신은 [자바 스크립트의 문 "을"에 대한 합법적 인 사용이 있습니까?]의

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

하지만 http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/을 사용해서는 안됩니다. – jarmod