How do I inherit javascript functions ? How do I inherit javascript functions ? javascript javascript

How do I inherit javascript functions ?


I hope that I'm understanding this right.

I believe you want a functor that's both an instance of a predefined prototype (yes, a class, just not a classic class) as well as directly callable? Right? If so, then this makes perfect sense and is very powerful and flexible (especially in a highly asynchronous environment like JavaScript). Sadly there is no way to do it elegantly in JavaScript without manipulating __proto__. You can do it by factoring out an anonymous function and copying all of the references to all of the methods (which seems to be the direction you were heading) to act as a proxy class. The downsides to this are...

  1. It's very costly in terms of runtime.
  2. (functorObj instanceof MyClass) will never be true.
  3. Properties will not be directly accessible (if they were all assigned by reference this would be a different story, but primitives are assigned by value). This can be solved with accessors via defineProperty or simply named accessor methods if necessary (it appears that that is what you're looking for, just add all properties to the functor with defineProperty via getters/setters instead of just functions if you don't need cross-engine support/backwards compatability).
  4. You're likely to run into edge cases where final native prototypes (like Object.prototype or Array.prototype [if you're inheriting that]) may not function as expected.
  5. Calling functorObj(someArg) will always make the this context be the object, regardless of if it's called functorObj.call(someOtherObj, someArg) (this is not the case for method calls though)
  6. Because the functor object is created at request time, it will be locked in time and manipulating the initial prototype will not affect the allocated functor objects like a normal object would be affected (modifying MyClass.prototype will not affect any functor objects and the reverse is true as well).

If you use it gently though, none of this should be a big deal.

In your prototype of your class define something like...

// This is you're emulated "overloaded" call() operator.MyClass.prototype.execute = function() {   alert('I have been called like a function but have (semi-)proper access to this!');};MyClass.prototype.asFunctor = function(/* templateFunction */) {   if ((typeof arguments[0] !== 'function') && (typeof this.execute !== 'function'))      throw new TypeError('You really should define the calling operator for a functor shouldn\'t you?');   // This is both the resulting functor proxy object as well as the proxy call function   var res = function() {      var ret;      if (res.templateFunction !== null)         // the this context here could be res.asObject, or res, or whatever your goal is here         ret = res.templateFunction.call(this, arguments);      if (typeof res.asObject.execute === 'function')         ret = res.asObject.execute.apply(res.asObject, arguments);      return ret;   };   res.asObject = this;   res.templateFunction = (typeof arguments[0] === 'function') ? arguments[0] : null;   for (var k in this) {      if (typeof this[k] === 'function') {         res[k] = (function(reference) {            var m = function() {               return m.proxyReference.apply((this === res) ? res.asObject : this, arguments);            };            m.proxyReference = reference;            return m;         })(this.asObject[k]);      }   }   return res;};

Resulting usage would look something like...

var aobj = new MyClass();var afunctor = aobj.asFunctor();aobj.someMethodOfMine(); // << worksafunctor.someMethodOfMine(); // << works exactly like the previous call (including the this context).afunctor('hello'); // << works by calling aobj.execute('hello');(aobj instanceof MyClass) // << true(afunctor instanceof MyClass) // << false(afunctor.asObject === aobj) // << true// to bind with a previous function...var afunctor = (new MyClass()).asFunctor(function() { alert('I am the original call'); });afunctor() // << first calls the original, then execute();// To simply wrap a previous function, don't define execute() in the prototype.

You could even chain bind countless other objects/functions/etc until the cows came home. Just refactor the proxy call a bit.

Hope that helps. Oh, and of course you could change the factory flow so that a constructor called without the new operator then instantiates a new object and returns the functor object. However you prefer (you could surely do it other ways too).

Finally, to have any function become the execution operator for a functor in a bit more elegant of a manner, just make the proxy function a method of Function.prototype and pass it the object to wrap if you want to do something like (you would have to swap templateFunction with this and this with the argument of course)...

var functor = (function() { /* something */ }).asFunctor(aobj);


With ES6 it's possible to inherit from Function, see (duplicate) question

javascript class inherit from Function class

default export Attribute extends Function {...}