I am struggling with some basic Javascript knowledge, to build a new node where I need modules for the first time extensively:
In file myFunctionFile.js, a function is exported:
module.exports = function myFunction(config) {
this.existingFunctionality = function() {};
}
After loading that function, new extraFunctionality is added and then the function is stored in a Map:
var functions = new Map();
var myFunction = require('myFunctionFile.js');
myFunction.prototype.extraFunctionality = function() {};
objects.set('myFunction', myFunction);
The function object will be instantiated later in the process:
var storedFunction = functions.get('myFunction');
var functionObject = new storedFunction();
But functionObjectonly contains existingFunctionality, but notextraFunctionality.
An extensive search on Google didn't help me. Don't see what I am doing wrong, compared to the code examples that I have found... I don't understand why the new doesn't take into account my prototype changes
Thanks again Steve!!!
Yes I couldn't call it. But I provided a simplified example here. Will need to investigate further this evening, and then I will get back here with more info.
Have a nice sunday!
As you can see above it is only a "simplified" code snippet. I extracted only the lines relevant to this issue. Try to imagine a few thousands lines of Javascript code in between...
Hey @Steve-Mcl,
I still couldn't call the extraFunctionality function this evening in my real node code, but it worked fine in the simplified version like you already pointed out. Seemed after all that my node was calling the code (to add the extraFunctionality function to the prototype) too late in the process.
But thanks to your confirmation that my code should work, I manage to troubleshoot and solve it by simply moving my code snippet to another location...
I really appreciate that you took the time to test my code snippet in VsCode!!!!!!!!
Bart
You exported a function in an external module, then added another function to the imported module using a prototype override then you attached that compound object to a map and then instantiate a copy of the compound object later on.
The node reads all the files and builds a catalog of functions:
var functionCatalog = new Map();
for (var i = 0; i < functionFileNames.length; i++) {
var customFunction = require(functionFileNames[i]);
functionCatalog.set(functionFileNames[i], customFunction);
}
Later on the node uses the functionCatalog in all kind of places (in the config screen, in the calculations, ...).
Moreover all the CustomFunctions need to contain a shared function. Since I want to keep the files as simple as possible, so I wanted to add the shared functions in step 2:
You would have to test whether the prototype addition works though. I think it would but I'm not as much of an expert as my ego would make out! Actually, thinking about it, if the input module is an object, you can simply add the shared function to the object, you don't need to mess with the prototype at all.
I'm not totally clear if you are trying to handle files from multiple users or multiple files from 1 user. If the latter, if you could stipulate that the function names have to be unique across those user files, I think you could also get rid of the map? You would just be able to merge the objects. You could even make the merge more intelligent by either throwing a warning on a duplicate name or just adding a number to it until it goes in.
Of course, I've probably completely mangled your idea!
Yes that is easier to understand.
I had started from the example of a Node-RED node:
module.exports = function(RED) {
...
}
But indeed for my use case a simple object is better...
Even better ...
Me neither. It is currently only a POC, so I will keep the map at the moment. Let's see how it evolves....
Absolutely
But in this early stage I don't mind at all. I prefer to have the basics correctly ...
The only thing I'm wondering now: I can have N instances of a single CustomFunction1 in my node. Previously this was easy, because by calling the CustomFunction1 it was filled with instance data. But now I have to start cloning that object from my catalog. Need to think about that.