javascript - Use RequireJS to include a class defined using ES6 syntax -


i have existing project (which run in browser, no server-side component) uses requirejs manage dependencies. way i'd been defining "classes" (including inheritance) this:

define([     'jquery',     'classes/someparentclass', ], function($, someparentclass){      function thisclass(data){          someparentclass.call(this, data);          this.init = function(data){             //do stuff         }          this.init(data);     }      thisclass.prototype = object.create(someparentclass.prototype);     thisclass.prototype.constructor = thisclass;      return thisclass;  }); 

and when wanted include in other class i'd use standard requirejs define/require syntax:

define(['classes/thisclass'], function(thisclass){     var fooinstance = new thisclass(foodata); }); 

i wanted try hand @ "class" syntax in es6, set converting 1 of existing classes (i started "static" class simplicity). looks this:

define([     'jquery', ], function($){      class someclass {          static somestaticfn(){             //do whatever         }     }  }); 

where i'm stymied how reference someclass (with new es6 syntax) using requirejs. assuming that's possible.

update

as others have pointed out below, returning class requirejs cares about. have sworn tried that, sure enough worked.

in case else dummy me , ends here under similar circumstances, want point out did have success requirejs-babel plugin, after i'd re-written class use es6 export , import statements instead of requirejs's define. after having done that, anywhere else including class had so:

define(['es6!thisclass'], function(thisclass){ ... 

i ended choosing modify class included in every other module, adding "es6!" strings turned out quite drag. although this article on porting existing projects es6 mentions way of automating process via requirejs config.

however, requirejs-babel transpiles es6 code @ runtime, not incorporate project @ time. if decide want go whole hog es6 i'd use "regular" babel or possibly typescript

es6 class sugarcoat , still same implementation underneath. can use old way creating class function. careful browser compatibility https://kangax.github.io/compat-table/es6/.

define([     'jquery', ], function($){      return class someclass {          static somestaticfn(){             //do whatever         }     }  }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -