2013-09-25 2 views
0

편집 : 내부 함수가 함수에 전달 된 추가 인수를 '취하는 방식'을 더 잘 이해하고 싶습니다. 예를 들어, 함수는 하나의 인수를 필요로하고 우리는 그것을 3 개로 지정합니다. 다른 두 인수는 어디에 있습니까?). 이 질문을 위해서, 나는 arguments 객체를 사용하는 것을 피하고 싶다.자바 스크립트에서 선택적 인수 'fall through'기능은 무엇입니까?

내부 함수에 대한 선택적 인수가 어떻게 떨어지는지를 더 잘 이해하고 싶습니다. 내가 예를 들어 아래의 코드를 사용합니다 :

function outside() { 
    var x = 10; 
    function inside(x) { 
     return x; 
    } 
    function inside2(a) { 
     return a; 
    } 
    return inside2; // or inside, doesn't seem to make a difference here 
} 
outside()(20,5) 

에 관계없이 외부는() inside2 또는 inside1 반환 여부, 수 (20)는 항상 반환됩니다. 다른 내부 기능을 위해 추가 입력을 활용할 수있는 방법이 있습니까? 예를 들어

이 (잘못된 코드) 같은 :

function outside() { 
    var x = 10; 
    function inside(x) { 
     return x; 
    } 
    function inside2(a) { 
     return inside(); 
    } 
    return inside2; 
} 
outside()(20,5) 

답변

0

난 당신이 arguments 변수를 찾고 생각합니다.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments

var someFunc = function(arg1) { 
    console.log(arguments[1]); //logs 'second arg' 
    console.log(arguments.length); //logs 2 
}; 

someFunc('first arg','second arg'); 

편집

그것은 당신이 무엇을 의미하는지 정확히 해독하기 어렵다,하지만 난 당신이 관심이 무엇을 할 수있는 다음의 라인을 따라 뭔가를 생각하고 있어요 :

function outside() { 
    var x = 10; 
    function inside(x) { 
     return x; 
    } 
    function inside2(a) { 
     if(arguments.length == 2) { 
     //return the 'inside' function with the second parameter 
     return inside(arguments[1]); 
     } 

     return a; 
    } 
    return inside2; 
} 
outside()(20,5) 
0

변수는 glob가 될 때까지 부모 범위의 변수를 연속적으로 확인하여 해결됩니다. 알 범위에 도달했습니다. 그 값이 정의되지 비록 두 번째 예에서

insideinside2 내에 x 변수가 이미 정의되어 호출 될 때. 런타임 캠은 inside의 함수 범위에서 변수를 찾으므로 outside 또는 전역 범위에서 값을 가져 오려고 시도하지 않습니다.

당신은 외부 변수의 이름을 변경하고 같은 것을 할 수 있습니다 : 함수는 외부 함수를 반환

default_x = 10; 
function inside(x){ 
    x = x || default_x; // Return argument x if it's value is truthy, otherwise use default 
    return x 
} 
0

. 이 두 함수는 모두 동일하므로 둘 다 똑같은 것을 반환한다는 것은 놀라운 일이 아닙니다. 내 생각 엔 내부가 클로저의 일부로 x 값을 반환 할 것으로 예상하고 있다고 생각합니다. 문제는 동일한 변수 이름으로 함수 레벨 변수를 정의하여 효과적으로 클로저 값을 대체하는 것입니다. 내부의 변수 이름의 이름을 다른 이름으로 변경하면 예상 한 것을 볼 수 있다고 생각합니다.

3

JS에서 함수, 변수 및 매개 변수가 어떻게 작동하는지 이해하는 데있어 근본적인 문제가 있다고 생각합니다. 나는 당신을 계몽하기를 바라고 코드를 설명합니다 : 당신은 더 나은 당신이 당신의 코드를 달성하기 위해 원하는 것을 설명하고자하는 경우

function outside() { 
    var x = 10; // variable x is a local variable in the scope of the outside function. it is not used anywhere 
    function inside(x) { 
     // parameter x hides the variable in the outer scope (outside) with the same name 

     // this function returns the first parameter passed to it. 
     // what the value returned is will be decided when the function is called 
     return x; 
    } 
    function inside2(a) { 
     // this function also returns the first parameter passed to it. 
     return a; 
    } 
    return inside2; // or inside, doesn't seem to make a difference here 
    // no difference because both functions do the same thing 
} 
outside()(20,5) // this code is equivalent to the following lines: 

var temp = outside(); // temp is the return value of outside - in our case the inside2 function 
temp(20,5); // the inside2 function is called with 2 arguments. It returns the first one (20) and ignores the other one 

이 이렇게하시기 바랍니다 그리고 난 당신이 그렇게 도움이 될 것입니다.한편, MDN에서 기능과 범위에 대한 몇 가지 읽기 : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope

편집 : 당신 때문에

function outside(x,a) { 
    function inside1() { 
    return x; // returns the x from the outer scope = the first parameter passed to outside 
    } 
    function inside2() { 
    return a; // returns the a from the outer scope = the second parameter passed to outside 
    } 
    return inside2; 
} 

outside(20,5)() // returns 5 

JsBin :는 일부 추측 후, 나는 아마이 같은 작업을 수행 할 수 있습니다 생각 그것으로 바이올린 수 있습니다 : http://jsbin.com/oKAroQI/1/edit