---
layout: page
title: "JavaScript runkit_function_add function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/runkit_function_add:822
- /functions/view/runkit_function_add
- /functions/view/822
- /functions/runkit_function_add:822
- /functions/822
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's runkit_function_add

{% codeblock runkit/runkit_function_add.js lang:js https://raw.github.com/kvz/phpjs/master/functions/runkit/runkit_function_add.js raw on github %}
function runkit_function_add (funcname, arglist, code) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %          note 1: Function can only be added to the global context; use create_function() for an anonymous function
  // *     example 1: runkit_function_add('add', 'a, b', "return (a + b);");
  // *     returns 1: true
  if (this.window[funcname] !== undefined) { // Presumably disallows adding where exists, since there is also redefine function
    return false;
  }

  try {
    this.window[funcname] = Function.apply(null, arglist.split(',').concat(code));
  } catch (e) {
    return false;
  }
  return true;
}
{% endcodeblock %}

 - [view on github](https://github.com/kvz/phpjs/blob/master/functions/runkit/runkit_function_add.js)
 - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/runkit/runkit_function_add.js)

### Example 1
This code
{% codeblock lang:js example %}
runkit_function_add('add', 'a, b', "return (a + b);");
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}


### Other PHP functions in the runkit extension
{% render_partial _includes/custom/runkit.html %}
