# Defining and reuse a "classic" JS function

**URL:** https://discourse.nodered.org/t/defining-and-reuse-a-classic-js-function/16372
**Category:** General
**Created:** [6 October 2019 07:22 UTC](https://discourse.nodered.org/t/defining-and-reuse-a-classic-js-function/16372 "2019-10-06T07:22:29Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![BruceFeuillette](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brucefeuillette/32/12869_2.png) [@BruceFeuillette](https://discourse.nodered.org/u/BruceFeuillette)
#### Post date: [6 October 2019 09:33 UTC](https://discourse.nodered.org/t/defining-and-reuse-a-classic-js-function/16372/10 "2019-10-06T09:33:40Z")

</div>

Works well !  
I've done a mix with a subflow. 🙂  
My subflow, called "JS classic function", has an Inject node "trigger-on-start" and 2 functions (hex2bin and hex2dec) wired to it, so it's executed at 0.1s after Node-Red start and the functions are in memory.  
I just had tyo had my Subflow to the flow to enable the classic JS functions.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/57cce4aa3681311cc93e1bc1c45b23e5b682659a.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/44ab64f717c2466de642bbf57d394e8ff96d10f6.png)

Here's the code, if somebody is trying to do the same. 🙂

```auto
[{"id":"f9586397.5973","type":"subflow","name":"JS functions","info":"","category":"","in":[],"out":[],"env":[],"color":"#DDAA99"},{"id":"7d7eee51.7878a8","type":"function","z":"f9586397.5973","name":"hex2dec","func":"global.set('hex2dec', function(hexa) {\n return parseInt(hexa, 16);\n}, 'memoryOnly');\n","outputs":1,"noerr":0,"x":400,"y":80,"wires":[[]]},{"id":"2012dbe6.92df34","type":"function","z":"f9586397.5973","name":"hex2bin","func":"global.set('hex2bin', function(hex) {\n return (\"00000000\" + (parseInt(hex, 16)).toString(2)).substr(-8);\n}, 'memoryOnly');","outputs":1,"noerr":0,"x":400,"y":40,"wires":[[]]},{"id":"df964a73.694328","type":"inject","z":"f9586397.5973","name":"Trigger on start","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":140,"y":40,"wires":[["2012dbe6.92df34","7d7eee51.7878a8"]]},{"id":"35ca0b72.bb35ec","type":"tab","label":"Test functions","disabled":false,"info":""},{"id":"92ec9209.762ac","type":"inject","z":"35ca0b72.bb35ec","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":120,"wires":[["3c377357.2eb52c"]]},{"id":"3c377357.2eb52c","type":"function","z":"35ca0b72.bb35ec","name":"Test 2 functions","func":"msg.payload = {\n bin: global.get('hex2bin', 'memoryOnly')(\"0x777\"), \n dec: global.get('hex2dec', 'memoryOnly')(\"0x777\") \n}\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":120,"wires":[["867de411.01ff3"]]},{"id":"ce6ea1e.e1e90e","type":"subflow:f9586397.5973","z":"35ca0b72.bb35ec","name":"JS Classic functions","env":[],"x":170,"y":60,"wires":[]},{"id":"867de411.01ff3","type":"debug","z":"35ca0b72.bb35ec","name":"Full msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":520,"y":120,"wires":[]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/defining-and-reuse-a-classic-js-function/16372)._
