2012-04-25 6 views
1

私は古いJavascriptファイルの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(); 
});