public
Last active

  • Download Gist
message.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
function m(object, message, options) {
if (!object || !message || typeof (message) !== 'string') {
return null;
}
 
if (object === null || typeof (object) !== 'object') {
return null;
}
 
var reponse = null;
 
/* If the object doesn't respond to the message but defines behavior for
* forwarding, recursively forward the message */
 
if (typeof (object[message]) === 'undefined' && typeof (object['forwardInvocation']) !== 'undefined') {
response = m(object.forwardInvocation(), message, options);
 
} else if (typeof (object[message]) === 'function') {
response = object[message](options);
}
 
return response;
}
 
function class1() {
this.forwardInvocation = function () {
return object2;
};
}
 
function class2() {
this.forwardInvocation = function () {
return object3;
};
}
 
function class3() {
this.receivingFunction = function () {
return 'Message received.'
};
}
 
var object1 = new class1();
var object2 = new class2();
var object3 = new class3();
 
alert(m(object1, 'receivingFunction'));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.