2012-04-25 2 views
1

필자는 필자의 오래된 자바 스크립트 파일 중 하나를 requireJS와 호환되도록 이식하려고합니다. 이것은 이전에 코드가 어떻게 보이는지입니다. 이 모듈을 requireJS와 호환되도록 만드는 방법

// effect.js 
(function(exports){ 

    // shorthand 
    exports.effect = function(selector) { 
     return new Effect(selector); 
    }; 

    // init 
    exports.Effect = function(selector){ 
     this.target = document.getElementById(selector);   
    }; 

    Effect.prototype.run = function(){ 
     alert('halo'); 
    }; 
})(this); 

//invoke it with 
effect('box').run(); 

는 호환 requireJS로 만들려고 :.

// effect.js 
define(function(exports){ 
    // Shorthand 
    exports.effect = function(selector) { 
     return new Effect(selector); 
    }; 

    // init 
    exports.Effect = function(selector){ 
     alert('halo'); 
     this.target = document.getElementById(selector);   
    }; 

    Effect.prototype.run = function(){ 
     alert('halo'); 
    };  
} 

// require js 
require([ 
    'effect.js' 
],function(Effect){ 
    effect('box').run(); 
}) 
난 그냥 효과 ('상자')의 속기를 실행하는 동일한 결과를 얻을 어떻게 위의 코드는 실행되지 않습니다

실행().

+0

가 만족 인 경우 답을 동의하거나, 함께 환호 도움이 필요 더 이상 문제를 강조하세요 :) –

답변

2

이 시도 보내기

define(function() { 

    // init 
    var Effect = function(selector) { 
     this.target = document.getElementById(selector); 
    }; 

    Effect.prototype.run = function(){ 
     alert('halo'); 
    }; 

    // Replaces the 'shorthand' function from the first example 
    // This is returned as a module to the require call 
    return function(selector) { 
     return new Effect(selector); 
    } 

}); 

require(['effect.js'], function(effect) { 
    effect('box').run(); 
}); 
관련 문제