Is it possible to inherit from javascript Function object?

Is it possible to make my own function implementation in javascript? To have javascript object like php object with __invoke magic method?

  A = function(){};
  A.prototype = new Function();

  var a = new A();
  alert( a instanceof Function ); // true
  a(); // exception

Can’t answer my own question… There is an answer:

  • How to call a function after textbox lose focus
  • Dataloader for caching and Batching Database request
  • yielding from an iterator callback used inside a generator
  • How can I create an Asynchronous function in Javascript?
  • Function.prototype.call alters typeof this, outside strict mode; why?
  • Why does my code execute a function defined after the return statement?
  • Sorry for everebody, but I have found good way for my task (I was in several steps away from answer when posted question). I don’t delete question, hope somebody will spend less time then me.

      A = function()
      {
        var f = function() { alert( 'f called' ) };
        f.__proto__ = A.prototype;
        return f;
      };
    
      A.prototype = Function( );
      A.prototype.method = function() { alert( 'method called' ) };
    
      var a = new A();
      alert( a instanceof Function ); // true
      alert( a instanceof A ); // true
      a(); // f called
      a.method(); // method called 
    

    If it is newbie question pls send me email and I will delete it. Thank you!

  • How can I create an Asynchronous function in Javascript?
  • try {} without catch {} possible in JavaScript?
  • Remove a javascript function with Greasemonkey
  • Function.prototype.call alters typeof this, outside strict mode; why?
  • QML - getting the code of a JS function as a string
  • setInterval() not working
  • 2 Solutions collect form web for “Is it possible to inherit from javascript Function object?”

    How about this:

    function MyFunction ( f ) {
        _.extend( f, MyFunction.prototype );
        return f;
    };
    
    MyFunction.prototype.method = function () {
        alert( 'method called' );
    };
    
    var func = MyFunction( function () {
        alert( 'function called' );
    });
    

    Live demo: http://jsfiddle.net/nZczW/

    So, you create your custom functions by passing them to your custom function constructor:

    var func = MyFunction( function () {...} );
    

    The methods are defined (normally) on the constructor’s prototype:

    MyFunction.prototype.foo = function () {...};
    

    Inside the constructor, the methods are assigned directly on the function itself. (I used underscore.js’s _.extend()). So, your functions still inherit directly from Function.prototype.

    You can of course simplify this pattern:

    var methods = { /* hash of methods */ };
    
    var func = _.extend( function () { ... }, methods );
    

    Wouldn’t it be nice? There’s no good way to do it, unfortunately.

    The issue, as you have probably noticed, is that setting a function as a constructor function’s prototype and then invoking the constructor function with new results in a plain old object, not a callable function object.

    There is one possible semi-workaround, depending on what you need this for. It’s an interesting trick, but probably not a good idea for real-world use. You can create an iframe and grab the Function from the iframe’s window. Now you have a “copy” of Function that you can do whatever you want to without dirtying up your “real” Function function.

    var frame = document.createElement('iframe');
    frame.id = frame.name = 'hacks';
    frame.style.zIndex = '-1';
    document.appendChild(frame);
    var MyFunction = frames['hacks'].Function;
    // add stuff to MyFunction and MyFunction.prototype