I came to this question because I was looking for a way to fail on another object if the method was not present on the first object. It is not as flexible as what you ask - for example, if a method is missing, then it will fail.
I was thinking of doing this for the small library that I have, which helps to customize extjs objects in a way to make them more verified. I had separate calls to actually get objects to interact with and thought that this might be a good way to connect these calls, effectively returning an extended type
I can imagine two ways to do this:
Prototypes
You can do this using prototypes, as the material falls into the prototype if it is not on the object itself. It doesn't seem to work if the set of functions you want to use to use this keyword is obviously your object does not know or does not care about things that the other knows about.
If all of its own code and you don’t use this and the constructors ... which is a good idea for many reasons, you can do it like this:
var makeHorse = function () { var neigh = "neigh"; return { doTheNoise: function () { return neigh + " is all im saying" }, setNeigh: function (newNoise) { neigh = newNoise; } } }; var createSomething = function (fallThrough) { var constructor = function () {}; constructor.prototype = fallThrough; var instance = new constructor(); instance.someMethod = function () { console.log("aaaaa"); }; instance.callTheOther = function () { var theNoise = instance.doTheNoise(); console.log(theNoise); }; return instance; }; var firstHorse = makeHorse(); var secondHorse = makeHorse(); secondHorse.setNeigh("mooo"); var firstWrapper = createSomething(firstHorse); var secondWrapper = createSomething(secondHorse); var nothingWrapper = createSomething(); firstWrapper.someMethod(); firstWrapper.callTheOther(); console.log(firstWrapper.doTheNoise()); secondWrapper.someMethod(); secondWrapper.callTheOther(); console.log(secondWrapper.doTheNoise()); nothingWrapper.someMethod();
This does not work for my use case, as the guys from extjs not only mistakenly used 'this', they also created a whole crazy system of the classical type of inheritance, using prototypes and 'this'.
This is the first time I've used prototypes / constructors, and I was a little puzzled that you cannot just install the prototype - you will also have to use the constructor. There is a magic field in objects (at least in firefox) that calls __proto, which is basically a real prototype. it seems that the actual prototype field is only used during construction ... how confusing!
Copy methods
This method is probably more expensive, but seems more elegant to me, and will also work with code using this (for example, so that you can use it to wrap around library objects). In addition, he will work with materials written using a functional / closing style. I just illustrated this with these / constructors to show that it works with such things.
Here are the mods:
//this is now a constructor var MakeHorse = function () { this.neigh = "neigh"; }; MakeHorse.prototype.doTheNoise = function () { return this.neigh + " is all im saying" }; MakeHorse.prototype.setNeigh = function (newNoise) { this.neigh = newNoise; }; var createSomething = function (fallThrough) { var instance = { someMethod : function () { console.log("aaaaa"); }, callTheOther : function () { //note this has had to change to directly call the fallThrough object var theNoise = fallThrough.doTheNoise(); console.log(theNoise); } }; //copy stuff over but not if it already exists for (var propertyName in fallThrough) if (!instance.hasOwnProperty(propertyName)) instance[propertyName] = fallThrough[propertyName]; return instance; }; var firstHorse = new MakeHorse(); var secondHorse = new MakeHorse(); secondHorse.setNeigh("mooo"); var firstWrapper = createSomething(firstHorse); var secondWrapper = createSomething(secondHorse); var nothingWrapper = createSomething(); firstWrapper.someMethod(); firstWrapper.callTheOther(); console.log(firstWrapper.doTheNoise()); secondWrapper.someMethod(); secondWrapper.callTheOther(); console.log(secondWrapper.doTheNoise()); nothingWrapper.someMethod(); //this call fails as we dont have this method on the fall through object (which is undefined) console.log(nothingWrapper.doTheNoise());
I really expected to use bind somewhere, but it doesn't seem to be necessary.