2012-02-04 3 views
0

추적한다 코드의 간단한 조각 :ActionScript 3은 단순한 동기 아키텍처 언어가 아니어야합니까?

  • 완료
  • 시험 리엔!

내가 완전히 멀리 그에서 뭔가를 얻을

시나리오 A :

var __functions_to_execute:Array; 

function start():void { 
    __functions_to_execute =[]; 

    __functions_to_execute.push(futile_trace()); 
    __functions_to_execute.push(futile_trace('test')); 

    execute_functions(); 
} 

function execute_functions():void { 
    if(__functions_to_execute.length){ 
     //where shift on this Array remove the first element and returns it 
     var exec:Function =__functions_to_execute.shift(); 
     exec; 

     //I tried this too, just in case 
     //__functions_to_execute[0]; 
     //__functions_to_execute.shift(); 
    } else trace("done!"); 
} 

function futile_trace(_value:String ='rien'):void { 
    trace(_value); 
    execute_functions(); 
} 

start(); 

아주 간단합니다. 하지만 결과는 다음과 같습니다

  • 완료

    • 리엔!

      function futile_trace(_value:String ='rien'):void { 
          trace(_value); 
          setTimeout(execute_functions, 0); 
      } 
      

      후 결과는 다음과 같습니다 :

    • 테스트

    이에 사용되지 않는 기능을 추가로 futile_trace 기능을 변경할 수 있습니다

    • 은 리엔
    • 시험
    • 완료!

    확인 후, 나 자신에게 말했다는 왜, 내가 execute_functions를 호출 할 때 범위를 변경할 수 있습니다, 그래서 시도 :

    function futile_trace(_value:String ='rien'):void { 
        trace(_value); 
        extra_step(); 
    } 
    
    function extra_step():void { 
        execute_functions(); 
    } 
    

    이 결과 무슨 생각?! 그래 :

    • 리엔
    • 완료!
    • 테스트

    그래서?! 추적 기능이 좋지 않습니까? 느린거야? 함수에 인수를 전달하면 다른 함수와 비교할 때 너무 많은 시간이 걸리는 것은 사실입니까? 내 말은 ... 와우!

    내가 이런 종류의 이상한 일을 피할 수있는 방법이 있습니까?

    은 (공식적으로, 내 프로젝트는 {리엔, 수행 및 테스트} ... 내가 "추적 문을 생략"로 컴파일하면 완전히 다르게 반응 코드의 15,000 라인을 가지고 여부를 추적 할 수 없습니다.

    귀하의 의견들에 대한

    감사합니다.

  • 답변

    3

    당신은 기능을 실행하고 __functions_to_execute 배열,하지에 기능 자체를 자신의 반환 값을 추가한다.

    함수 execute_functions 실제로 아무것도하지 않습니다.순서를 인라인으로 설명하려고 시도했습니다.

    function start():void { 
        __functions_to_execute =[]; 
        // 1. traces 'rien' first because futile_trace() is called with no args 
        // 2. 'done!' will be traced inside execute_functions because the array is still empty 
        // 3.undefined will be pushed into the array next 
        __functions_to_execute.push(futile_trace()); 
        // 4. traces 'test' 
        // execute_functions does not trace anything because __functions_to_execute is non-empty 
        // but it also doesn't do anything because it is just removing the `undefined` value from the start of the array. 
        __functions_to_execute.push(futile_trace('test')); 
    
        execute_functions(); 
    } 
    

    더 자세히 알려면 다음과 같이하십시오. 함수가 호출 될 때 전달되어야하는 인수와 함께 배열 함수 참조에 저장됩니다. 당신은 (함수 이름 뒤에 () 통지)를 호출하고, 다음에 그 호출의 결과를 밀어 - 실제로 이제까지 배열에 futile_trace을 밀어하지 시나리오 A에 대한

    var __functions_to_execute:Array; 
    
    function start():void { 
        __functions_to_execute = []; 
    
        __functions_to_execute.push({func:futile_trace, args:[]}); 
        __functions_to_execute.push({func:futile_trace, args:['test']}); 
    
        execute_functions(); 
    } 
    
    function execute_functions():void { 
        if(__functions_to_execute.length){ 
         var obj:Object = __functions_to_execute.shift(); 
         obj.func.apply(null, obj.args); 
        } else trace("done!"); 
    } 
    
    function futile_trace(_value:String ='rien'):void { 
        trace(_value); 
        execute_functions(); 
    } 
    
    start(); 
    
    +0

    덕분에 남자와 결과 (void)를 실제 함수가 아닌 배열에 저장하십시오! 젠장, 오케이, 그래서 나는 디버거를 사용하여 @Francis 쌍 (func, args) – Francis

    +0

    배열 배열을 사용하여이 테스트를 계속할 것이다. –

    +0

    내가 제안한 코드가 너무 단순 해 보이기 때문에 추적 또는 기타 이상한 비동기 이벤트가 발생할 것으로 의심되었습니다. 하지만 당신 말이 맞아요. 디버그를 사용해야합니다. 디버그에 관해서는 다른 질문을하겠습니다 : 마지막 프로젝트는 Flash Player 10.0이 설치된 Linux에서 Firefox 3.0에서 실행되는 독립 실행 형 응용 프로그램입니다. 어떤 도구를 파일로 실행하는 플래시 컨테이너를 비방하는 데 사용해야합니까? (실행중인 아파치가 없습니다. 바로 플래시를 로컬 경로에서 바로 파이어 폭스로 재생합니다)? 어떤 제안이라도 환영합니다.감사합니다 – Francis

    0

    , 당신이있어 정렬. 즉

    :

    1. 당신은 당신이 값을 통과하지 않기 때문에 ,을 '리엔'futile_trace()
    2. futile_trace 흔적을 호출합니다.
    3. futile_trace는 아무것도 그래서 _execute_functions 흔적이 아직 추진되지 않은이 시점에서 _execute_functions
    4. 를 호출 '완료!'
    5. _execute_functions 반환합니다.
    6. _futile_trace 반품
    7. futile_trace() (void)의 결과가 푸시됩니다.
    8. 당신은 futile_trace('test')
    9. futile_trace() 출력 '테스트'를 호출합니다.
    10. futile_trace 통화 배열에서 _execute_functions
    11. _execute_functions 변화 void.
    12. _execute_functions는 실행 void; (아무것도 안하는)
    13. 등 등

    당신은 당신이하지 않은 있도록, 다른 함수에 함수를 통과하거나 변수에 대한 참조를 저장해야하는 경우 그것을 부른다.

    __functions_to_execute.push(futile_trace); 
    // Use an anonymous function to pass with arguments without executing: 
    __functions_to_execute.push(function() { futile_trace('test'); }); 
    

    ... 그리고 _execute_functions는 괄호 기억하십시오, 그것은 함수를 호출합니다 그의 문자 양식을 사용하여 함수를 저장하기 위해 노력하고, 지금은 너무 많은 의미를

    exec(); 
    
    +0

    두 답변 모두 좋다, 도와 줘서 고마워! 불행히도 단 하나의 좋은 대답만을 지정할 수 있습니다! 그러나 당신의 설명에 감사드립니다! :) – Francis

    +0

    걱정하지 마라. 나는 자바 스크립트를 가지고있다. 어쨌든 (정의되지 않은) 어쨌든 지금 고쳐져있다. ;-) – JimmiTh