# Struggling with function prototypes for new node

**URL:** https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869
**Category:** Developing Nodes
**Created:** [25 July 2021 06:53 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869 "2021-07-25T06:53:16Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 July 2021 06:53 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/1 "2021-07-25T06:53:17Z")

</div>

Hi folks,

I am struggling with some basic Javascript knowledge, to build a new node where I need modules for the first time extensively:

1. In file myFunctionFile.js, a function is exported:

```auto
module.exports = function myFunction(config) {
   this.existingFunctionality = function() {};
}

```

2. After loading that function, new `extraFunctionality` is added and then the function is stored in a Map:

```auto
var functions = new Map();

var myFunction = require('myFunctionFile.js');

myFunction.prototype.extraFunctionality = function() {};

objects.set('myFunction', myFunction);

```

3. The function object will be instantiated later in the process:

```auto
var storedFunction = functions.get('myFunction');
var functionObject = new storedFunction();

```

But `functionObject` _ **only** _ contains `existingFunctionality`, but _ **not** _ `extraFunctionality`.

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 🥴

Again any help is appreciated!

Bart

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 July 2021 07:50 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/2 "2021-07-25T07:50:01Z")

</div>

Have you tried calling the `extraFunctionality()` function?

In a quick test in vscode, the `myFunction` object doesnt show `extraFunctionality` ...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22491563106102ea657f778b09a82c1c5a7647be.png)

but it IS in the ` __proto__ ` ...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/032b02ed9dbcba46539850bfd4553e3e8a0e1868.png)

and it can be called...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90d5f02221887218481457a832c697e74022e864.png)

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 July 2021 08:04 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/3 "2021-07-25T08:04:47Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [25 July 2021 15:59 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/4 "2021-07-25T15:59:21Z")

</div>

Is it just me? That code seems extremely complex without much in the way of benefits. I think I must be missing something.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 July 2021 16:29 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/5 "2021-07-25T16:29:55Z")

</div>

Hi Julian,  
Yes I am afraid it is just you 😂

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...

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [25 July 2021 17:11 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/6 "2021-07-25T17:11:25Z")

</div>

It was more that you seem to be mixing a number of styles. I guess without understanding what you are trying to achieve, it is hard to understand.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 July 2021 17:23 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/7 "2021-07-25T17:23:34Z")

</div>

> [@TotallyInformation](#):
>
> you seem to be mixing a number of styles

Ok interesting. Can you please explain that a bit more in detail, to make sure I use the correct style for my new node.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 July 2021 20:45 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/8 "2021-07-25T20:45:37Z")

</div>

> [@BartButenaers](#):
>
> Yes I couldn't call it.

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [26 July 2021 20:14 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/9 "2021-07-26T20:14:30Z")

</div>

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.

Did I get that right?

Does that not sound somewhat labyrinthine to you?

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [26 July 2021 21:34 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/10 "2021-07-26T21:34:52Z")

</div>

@TotallyInformation,  
Thanks for digging into it!  
I will try to explain it, and perhaps you (or someone else) see some points for simplification.

1. The goal is to allow users to write their own functions into separate files. For example file CustomFunction1.js contains:

```auto
module.exports = function CustomFunction1() {
   this.getName = function() { ... };
   this.getIcon = function() { ... };
   this.getDescription = function() { ... };
   this.calculate = function() { ... };
}

```

2. The node reads all the files and builds a catalog of functions:

```auto
var functionCatalog = new Map();

for (var i = 0; i < functionFileNames.length; i++) {
   var customFunction = require(functionFileNames[i]);
   functionCatalog.set(functionFileNames[i], customFunction);
}

```

3. 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:

```auto
var customFunction = require(functionFileNames[i]);
customFunction.prototype.sharedFunction = function() { ... };
functionCatalog.set(functionFileNames[i], customFunction);

```

Hopefully it is a bit more clear now to understand what I want to achieve ...

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [26 July 2021 22:21 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/11 "2021-07-26T22:21:01Z")

</div>

> [@BartButenaers](#):
>
> Hopefully it is a bit more clear now to understand what I want to achieve

Thanks, that is much clearer now.

> [@BartButenaers](#):
>
> `module.exports = function CustomFunction1() {`

I don't think that needs to be a function. It can just be an object I think which should save some confusion.

```js
module.exports = {
    getName: function() { ... },
    ...
}

```

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!

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [27 July 2021 05:44 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/12 "2021-07-27T05:44:38Z")

</div>

> [@TotallyInformation](#):
>
> It can just be an object I think which should save some confusion.

Yes that is easier to understand.  
I had started from the example of a Node-RED node:

```auto
module.exports = function(RED) {
    ...
}

```

But indeed for my use case a simple object is better...

> [@TotallyInformation](#):
>
> you don't need to mess with the prototype at all

Even better ...

> [@TotallyInformation](#):
>
> not totally clear if you are trying to handle files from multiple users or multiple files from 1 user

Me neither. It is currently only a POC, so I will keep the map at the moment. Let's see how it evolves....

> [@TotallyInformation](#):
>
> I've probably completely mangled your idea!

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.

But now off to work...

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 August 2021 05:45 UTC](https://discourse.nodered.org/t/struggling-with-function-prototypes-for-new-node/48869/13 "2021-08-10T05:45:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
