User's Guide

JavaScript Modules
The OVMS JavaScript engine supports the concept of modules (using the node.js style of
exports). Such modules can be written like this:
exports.print = function(obj, ind) {
var type = typeof obj;
if (type == "object" && Array.isArray(obj)) type = "array";
if (!ind) ind = '';
switch (type) {
case "string":
print('"' + obj.replace(/\"/g, '\\\"') + '"');
break;
case "array":
print('[\n');
for (var i = 0; i < obj.length; i++) {
print(ind + ' ');
exports.print(obj[i], ind + ' ');
if (i != obj.length-1) print(',');
print('\n');
}
print(ind + ']');
break;
case "object":
print('{\n');
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
print(ind + ' "' + keys[i] + '": ');
exports.print(obj[keys[i]], ind + ' ');
if (i != keys.length-1) print(',');
print('\n');
}
print(ind + '}');
break;
default:
print(obj);
}
if (ind == '') print('\n');
}
By convention, modules such as this are placed in the /store/scripts/lib directory as
<modulename>.js. These modules can be loaded with:
JSON = require("lib/JSON");
OVMS v3 User Guide Page 48 / 86