I'm not sure I explained myself correctly. I have a generic class ussed by lots of *.js files let´s said TestClass.
class TestClass {
constructor(a,b) {
this.a = a || 0;
this.b = b || 0;
}
// methods
suma(a,b)
{
return a+b;
}
}
What I need is to use this "classic" class from several *.js files builded using "module pattern"
//const {moduloTest} = require("scripts/testClass.js"); doesn´t work even using the answer in How do I include a JavaScript file in another JavaScript file?
//import{TestClass} from "scripts/testClass.js"; doesn´t work ( even with the extension *.mjs)
example file :
var MyNameSpace = {};
MyNameSpace = (function () {
// global variables
var object1 = new TestClass();
// Private methods
function PrivateMethod () {
console.log("result = ", object1.suma(3,4));
}
// ..........................................................
// public methods
return {
init: function () {},
anotherPublicMethod: function () {}
}
}());
new edition to show how I had already included the call to namespace in a very simple html code
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title> module pattern with testClass. </title>
</head>
<!--here the call.-->
<body onload="moduloTest.init();">
<script src="scripts/ClasePrueba.js"></script>
<script src="scripts/modulePattern.js"></script>
</body>
</html>