I am porting one of my old javascript file to compatible with requireJS. This is previously how the code looks like.
// 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();
Tried to make it compatible with 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();
})
The code above won’t run, how do I achieve the same result with just running shorthand of effect(‘box’).run().
Give this a try: