phpjs
Version:
56 lines (48 loc) • 1.82 kB
Markdown
layout: page
title: "JavaScript runkit_function_redefine function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/runkit_function_redefine:824
- /functions/view/runkit_function_redefine
- /functions/view/824
- /functions/runkit_function_redefine:824
- /functions/824
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's runkit_function_redefine
{% codeblock runkit/runkit_function_redefine.js lang:js https://raw.github.com/kvz/phpjs/master/functions/runkit/runkit_function_redefine.js raw on github %}
function runkit_function_redefine (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: function add (a, b, c) {return a+b+c;}
// * example 1: runkit_function_redefine('add', 'a, b', "return (a + b);");
// * returns 1: true
if (this.window[funcname] === undefined) { // Requires existing 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_redefine.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/runkit/runkit_function_redefine.js)
### Example 1
This code
{% codeblock lang:js example %}
function add (a, b, c) {return a+b+c;}
runkit_function_redefine('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 %}