JavaScript runkit_function_redefine function

A JavaScript equivalent of PHP’s runkit_function_redefine
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;
}

Example 1
function add (a, b, c) {return a+b+c;}
runkit_function_redefine('add', 'a, b', "return (a + b);");

Should return
true
Теги:
runkit_function_redefine
Добавлено: 27 Июля 2018 20:21:25 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...