Prosty program obsługi wywołania zwrotnego, który napisałem dzisiaj jako przykład tego, w jaki sposób robię głębokie zagnieżdżanie. Przepraszam, jeśli to nie kolana pszczół, jeśli chodzi o styl kodowania, sprawiło, że koncepcja stała się dla mnie nieco jaśniejsza.
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();
żywo: http://jsfiddle.net/Ps5Uf/
- UWAGA: Aby wyświetlić wyjścia konsoli, wystarczy otworzyć inspektora w chromie i kliknij na zakładkę "Konsola".
co chcesz osiągnąć? – NicoSantangelo
Próbuję emulować klas zagnieżdżonych, jak zwykle w językach programowania. Sprawia, że OOP jest bardzo zorganizowany, a biblioteki dużo bardziej zorganizowane. Wiem, że JavaScript nie jest tak naprawdę zbudowany dla złożonego OOP, ale niezależnie od tego pomaga. JavaScript ma bardzo niekonwencjonalne podejście do funkcji, ale mogę całkowicie zrozumieć, dlaczego jest tak potężny. –