Created
October 28, 2018 13:25
-
-
Save sawaYch/a4ec331f64850b47bf2c2f52f036d444 to your computer and use it in GitHub Desktop.
ES6 module, import, export equivalent code to achieve browser compatible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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