2012-11-22 2 views
1

프로토 타입 함수 name.argument와 함께 JavaScript 함수가 다음 프로그램에서 어떻게 작동합니까?프로토 타입 함수 name.argument와 JavaScript 함수 호출은 어떻게 작동합니까?

function getLAdd() { 
    // this sets all the variables containing positions of ball and bar with their respective ids. 
    var ladd = 0; 
    var pball = $("#ball"); 
    var pbar = $("#bar"); 
    var bar_position = pbar.position(); 
    var ball_position = pball.position(); 
    if (ball_position.top >= window.innerHeight - 100) { 
     if (ball_position.left - 10 >= bar_position.left && ball_position.left - 10 <= bar_position.left + 100) { 
      ladd = -2; 
     } 
     if (ball_position.left + 10 <= bar_position.left + 200 && ball_position.left + 10 >= bar_position.left + 100) { 
      ladd = 2; 
     } 
    } 
// how does getLAdd.ladd work ? Is this a type of dynamic call ? 
    if (ladd == 0) { 
     ladd = getLAdd.ladd; 
    } 
    if (ball_position.left <= 15 || ball_position.left >= window.innerWidth - 40) 
     ladd = -ladd; 

    getLAdd.ladd = ladd; 
    return ladd; 
} 

답변

3

JavaScript의 함수는 객체이므로 속성을 추가 할 수 있습니다.

ladd라는 속성이 getLAdd 기능에 추가 된 코드에서

,이 라인에서 검색되고 :

ladd = getLAdd.ladd; 

이 라인에서 업데이트된다

getLAdd.ladd = ladd; 

모든 기능을 사용하여 동일한 작업을 수행 할 수 있습니다.

function f() { 
     // get the property 
    console.log(f.foo); // bar 
} 

    // add a property to the function object 
f.foo = "bar"; 

    // get the property 
console.log(f.foo); // bar 

    // call the function 
f(); 
관련 문제