The following code works just fine, but provides me twice with a warning Module ABC not found when uploading to the board. Since I provide the module id and code, I would not expect any warnings on .addCached() nor on require().
For 1V71:
Modules.addCached("ABC"
, "exports.name = 'Espruino'; "
+"exports.spelled = function() { return exports.name.split('').join(' - '); }; "
);
var m;
console.log((m = require("ABC")).name); // --> Espruino
console.log(require("ABC").spelled()); // --> E - s - p - r - ...
m.name = "espruino";
console.log(require("ABC").spelled()); // --> e - s - p - r - ...
console.log(m.name); // --> espruino
console.log(m.spelled()); // --> e - s - p - r - ...
Above code proves that repeated require() return the very same module (object).
For *1v72 - which supports generic exports for more obvious object-oriented programming style with singletons and classes defined in/as modules - I use this code, and get the same complaints about Module XYZ not found:
Modules.addCached("XYZ" // Singleton
, "exports = "
+"{ name: 'Espruino' "
+", spelled: function() { "
+" return this.name.split('').join(' - '); } "
+"}; "
);
console.log(require("XYZ").name); // --> Espruinus
console.log(require("XYZ").spelled()); // --> E - s - p - r - ...
require("XYZ").name = "espruino"; // modifying module / singleton
console.log(require("XYZ").name); // --> espruino
console.log(require("XYZ").spelled()); // --> e - s - p - r - ...
Modules.addCached("Person" // Class
, "var Person = function(name) { "
+" this.name = name; "
+" }; "
+"Person.prototype.spelled = function() { "
+" return this.name.split('').join(' - '); }; "
+"exports = Person; "
);
// var Person = require("Person");
// var person = new Person("Espruinus");
var person = new (require("Person"))("Espruinus");
console.log(person.name); // --> Espruinus
console.log(person.spelled()); // --> E - s - p - r - ...
person.name = "Scriptus";
console.log(person.spelled()); // --> S - c - r - i - ...
require("Person").prototype.spelled
= function() { return this.name.split('').join(' = '); };
console.log(person.spelled()); // --> S = c = r = i = ...
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
The following code works just fine, but provides me twice with a warning Module ABC not found when uploading to the board. Since I provide the module id and code, I would not expect any warnings on .addCached() nor on require().
For 1V71:
Above code proves that repeated require() return the very same module (object).
For *1v72 - which supports generic exports for more obvious object-oriented programming style with singletons and classes defined in/as modules - I use this code, and get the same complaints about Module XYZ not found: