I'm trying to import a module into JavaScript. In python
, because it's simple, you just import what you want with import
.
In javascript
apparently also seems simple. In my case, I'm trying to implement the example that comes out in Using JavaScript code modules
However, I get this error in google chrome Uncaught ReferenceError: Components is not defined
. What will I be doing wrong?
my_module.jsm
var EXPORTED_SYMBOLS = ["foo", "bar"];
function foo() {
return "foo";
}
var bar = {
name : "bar",
size : 3
};
var dummy = "dummy";
test.js
Components.utils.import("resource://my_module.jsm");
alert(foo()); // displays "foo"
alert(bar.size + 3); // displays "6"
alert(dummy); // displays "dummy is not defined" because 'dummy' was not exported from the module
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>hola</h1>
<script src="test.js"></script>
</body>
</html>