Одна вещь, которую вы могли бы сделать, чтобы иметь «вызывающему» вызова функции передается в нее, и распечатать возвращаемое значение ,
function caller(func) {
function printMessage(message) {
console.log(message);
}
printMessage(func());
}
function callee() {
return 'hello world';
}
function callee2() {
return 'hello world 2';
}
caller(callee);
caller(callee2);
Редактировать
После прочтения ваших комментариев на вопрос, это может быть лучше подходит для ОО-подхода например:
//--- Base 'libary' caller ---//
var caller = function() {
}
//--- Add out printMessage function to our base libary ---//
caller.prototype.printMessage = function(message){
console.log(message);
}
//--- Users 'libary' callee ---//
var callee = function() {
caller.call(this);
//--- Our users libary has a printing function ---//
this.print = function (message) {
this.printMessage("callee > " + message);
}
}
callee.prototype = Object.create(caller.prototype); // Set callee's prototype to caller's prototype
callee.prototype.constructor = callee; // Set constructor back to callee
//--- Using the users libary ---//
var userLibary = new callee();
userLibary.print("hello world"); // 'callee > hello world'
//--- The original libary's print method is still accessible ---//
userLibary.printMessage("hello world"); // 'hello world'
Что вы пытаетесь достичь этим? Объект вроде 'console', который завершает реализацию? –
Функция вызывающего абонента будет частью библиотеки, при этом функция вызываемого абонента будет записана пользователем. printMessage является частью API, поэтому пользователи должны иметь возможность вызвать его в своем коде. – AmorphousCrystal