JS.AddFunction
--------------

Adds a global function defined as a text string.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [JavaScript](component_JavaScript.md) | [10.0](newinversion100.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "JS.AddFunction"; JSRef; Name; JavaScript ) 

**MBS**( **"JS.AddFunction";** /\* Adds a global function defined as a text string. \*/  
**$JSRef**; /\* The JavaScript reference number.e.g. $js \*/   
**$Name**; /\* The name of the function.e.g. "test" \*/   
**$JavaScript**) /\* The JavaScript code.e.g. "function (x,y) { return x+y; }" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| JSRef | The JavaScript reference number. | $js |
| Name | The name of the function. | "test" |
| JavaScript | The JavaScript code. | "function (x,y) { return x+y; }" |

### Result

Returns OK or error.

### Description

Adds a global function defined as a text string.  
Optional provide a file name to show in stack traces.  
Function source code may contain a function name, but we use only the name parameter to register the function as global one.  
### Examples

Define a function and call it:

 Set Variable \[ $js ; Value: MBS ( "[JS.New](JSNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "JS.AddFunction"; $js ; "adder"; "function (x,y) { return x+y; }" ) \]   
Show Custom Dialog \[ "Result" ; MBS ( "[JS.CallFunction](JSCallFunction.md)"; $JS ; "adder"; 3; 4 ) \]   
Set Variable \[ $r ; Value: MBS ( "[JS.Release](JSRelease.md)"; $JS ) \]  
Initialize JavaScript environment with two functions:

 If \[ Length($$js ) = 0 \]   
 Set Variable \[ $$js ; Value: MBS ( "[JS.New](JSNew.md)" ) \]   
 Set Variable \[ $r ; Value: MBS ( "JS.AddFunction"; $$js ; "makeCRCTable"; "function makeCRCTable(){  
 var c;  
 var crcTable = \[\];  
 for(var n =0; n &lt; 256; n++){  
 c = n;  
 for(var k =0; k &lt; 8; k++){  
 c = ((c&amp;1) ? (0xEDB88320 ^ (c &gt;&gt;&gt; 1)) : (c &gt;&gt;&gt; 1));  
 }  
 crcTable\[n\] = c;  
 }  
 return crcTable;  
}") \]   
 Set Variable \[ $r ; Value: MBS ( "JS.AddFunction"; $$js ; "crc32"; "function crc32(str) {  
 var crcTable = crcTable || (crcTable = makeCRCTable());  
 var crc = 0 ^ (-1);  
  
 for (var i = 0; i &lt; str.length; i++ ) {  
 crc = (crc &gt;&gt;&gt; 8) ^ crcTable\[(crc ^ str.charCodeAt(i)) &amp; 0xFF\];  
 }  
  
 return (crc ^ (-1)) &gt;&gt;&gt; 0;  
}") \]   
End If  
Register custom function for "js" JavaScript environment:

 # Register one function. Replacing older one with same name   
Set Variable \[ $r ; Value: MBS ( "JS.AddFunction"; "js"; JavaScriptCustomFunctions::Name ; "function(" &amp; Substitute(List ( JavaScriptCustomFunctionParameters::Name ); ¶; ", ") &amp; ") { " &amp; JavaScriptCustomFunctions::Body &amp; " }" ) \]   
If \[ MBS ("IsError") \]   
 Show Custom Dialog \[ "Failed to add function." ; $r \]   
End If  
### See also

- [IsError](IsError.md)
- [JS.CallFunction](JSCallFunction.md)
- [JS.CF](JSCF.md)
- [JS.New](JSNew.md)
- [JS.Release](JSRelease.md)

### Example Databases

- [JavaScript/CoordinatDistance](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/CoordinatDistance.shtml#1ScriptAnchor_)
- [JavaScript/FM JSON Path](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/FM%20JSON%20Path.shtml#1ScriptAnchor_)
- [JavaScript/JavaScript Custom Functions](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/JavaScript%20Custom%20Functions.shtml#3ScriptAnchor_)
- [JavaScript/JavaScript](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/JavaScript.shtml#3ScriptAnchor_)

### Blog Entries

- [New in MBS FileMaker Plugin 11.3](https://www.mbsplugins.de/archive/2021-07-27/New_in_MBS_FileMaker_Plugin_11/monkeybreadsoftware_blog_filemaker)
- [Custom Functions in JavaScript](https://www.mbsplugins.de/archive/2021-06-14/Custom_Functions_in_JavaScript/monkeybreadsoftware_blog_filemaker)
- [FileMaker and JavaScript - the perfect combination](https://www.mbsplugins.de/archive/2020-02-03/FileMaker_and_JavaScript_-_the/monkeybreadsoftware_blog_filemaker)
- [New in the MBS FileMaker Plugin 10.0](https://www.mbsplugins.de/archive/2020-01-23/New_in_the_MBS_FileMaker_Plugi/monkeybreadsoftware_blog_filemaker)
- [CRC function in FileMaker using JavaScript](https://www.mbsplugins.de/archive/2020-01-15/CRC_function_in_FileMaker_usin/monkeybreadsoftware_blog_filemaker)
- [Our JavaScript engine for FileMaker](https://www.mbsplugins.de/archive/2019-12-09/Our_JavaScript_engine_for_File/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 4/2021, Seite 24 bis 25](https://filemaker-magazin.de/neuigkeit/4152-Appetithappen-FMM_202104)
- [Ausgabe 4/2021, Seite 21](https://filemaker-magazin.de/neuigkeit/4152-Appetithappen-FMM_202104)

This function checks for a license.

Created 7th December 2019 , last changed 5th June 2021

  
[JS.AddFileMakerSQLFunction](JSAddFileMakerSQLFunction.md) - [JS.CF](JSCF.md)

[HTML Version](JSAddFunction.shtml)