How do I load a library?
I feel very foolish for asking such a supposedly mundane thing, but I couldn't find the answer on Ecma-262 or MDN.
Solution must work with d8 (V8), jjs (Nashorn), js (SpiderMonkey), rhino (Rhino), seed (JavaScriptCore).
asked Aug 8, 2014 at 14:34
daxim
39.3k4 gold badges72 silver badges135 bronze badges
-
3JavaScript has no native features for loading libraries. How you do it is host environment specific (but many support AMD).Quentin– Quentin2014年08月08日 14:37:37 +00:00Commented Aug 8, 2014 at 14:37
1 Answer 1
if ('undefined' === typeof(require)) {
if ('function' === typeof(load)) {
require = load;
} else if ('object' === typeof(imports)) {
require = function(library_name) {
eval(imports.gi.Gio.file_new_for_path(
library_name).read().get_contents());
}
} else {
throw('require not implemented');
}
}
require('./some_library.js');
console.log(some_library.some_function(some_parameters));
answered Aug 20, 2014 at 18:49
daxim
39.3k4 gold badges72 silver badges135 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js