Un sencillo gestor de devolución de llamada que escribí hoy como un ejemplo de cómo hago una anidación profunda. Me disculpo si no son las rodillas de las abejas cuando se trata de estilo de código, hizo que el concepto sea un poco más claro para mí.
function test() {
this.that = this;
this.root = this;
this.jCallback = new Array(new Array()); // 2d
this.jCallbackCount = -1;
this.str = "hello";
// Callback handler...
this.command = {
that : this, // let's keep a reference to who's above us on the food chain
root : this.root, // takes us back to the main object
// add : function() { var that = this; console.log(that.that.str); },
add : function(targetFnc, newFunc) {
var that = this;
var home = that.that; // pretty much root but left in as an example of chain traversal.
var root = this.root; // useful for climbing back up the function chain
// console.log(that.that.str);
home.jCallbackCount++;
// target, addon, active
home.jCallback[home.jCallback.length] = { 'targetFunc' : targetFnc, 'newFunc' : newFunc, 'active' : true, 'id': home.jCallbackCount};
console.log('cbacklength: ' + home.jCallback.length);
console.log('added callback targetFunction:[' + targetFnc + ']');
return home.jCallbackCount; // if we want to delete this later...
},
run : function(targetFnc) {
var that = this;
var home = that.that;
console.log('running callback check for: ' + targetFnc + ' There is : ' + (home.jCallbackCount + 1) + 'in queue.');
console.log('length of callbacks is ' + home.jCallback.length);
for(i=0;i < home.jCallback.length - 1;i++)
{
console.log('checking array for a matching callback [' + targetFnc + ']...');
console.log('current item: ' + home.jCallback[i]['targetFunc']);
if(home.jCallback[i]['targetFunc'] == targetFnc)
{
// matched!
home.jCallback[i]['newFunc']();
}
// console.log(that.that.jCallback[i].targetFunction);
}
}
};
}
test.prototype = {
say : function() {
var that = this;
console.log('inside');
// that.command('doSay');
that.command.run('doSay');
console.log(that.str);
}
} // end proto
// BEGIN TESTING **************************************************************************
// BEGIN TESTING **************************************************************************
// BEGIN TESTING **************************************************************************
var testing = new test();
testing.command.add('doSay', function() { console.log('213123123'); });
testing.command.add('doSay', function() { console.log('12sad31'); });
testing.command.add('doSay', function() { console.log('asdascccc'); });
testing.say();
vivo: http://jsfiddle.net/Ps5Uf/
- nota: para ver la salida de la consola, solo inspector abierta en cromo y haga clic en la pestaña "consola".
¿qué desea lograr? – NicoSantangelo
Estoy tratando de emular las clases anidadas como normalmente se ven en los lenguajes de programación. Hace que OOP sea muy organizado y las bibliotecas mucho más organizadas. Sé que JavaScript no está realmente diseñado para OOP complejo, pero independientemente de eso ayuda. JavaScript tiene un enfoque muy poco ortodoxo de las funciones, pero puedo ver completamente por qué es tan poderoso. –