Skip to content

Instantly share code, notes, and snippets.

@sawaYch
Created October 28, 2018 13:25
Show Gist options
  • Save sawaYch/a4ec331f64850b47bf2c2f52f036d444 to your computer and use it in GitHub Desktop.
Save sawaYch/a4ec331f64850b47bf2c2f52f036d444 to your computer and use it in GitHub Desktop.
ES6 module, import, export equivalent code to achieve browser compatible
// init function, just a test
function init() {
$(".accordion").on("click", ".accordion-header", function() {
$(this).toggleClass("active").next().slideToggle();
});
return true;
}
// run it when it;s ready
$(document).ready(init);
// browser compatible module export
if (typeof module !== 'undefined' &&
typeof module.exports !== 'underfined'){
module.exports= init;
}else{
if (typeof define === 'function' && define.amd){
define([], function(){ return init;});
}else
window.init = init;
}
// use require instead of import
const init = require('./init.js');
test('Init() Test', ()=> {
expect(init()).toBe(true);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment