Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   evaluation question (https://javascript.ru/forum/misc/28231-evaluation-question.html)

belbek 11.05.2012 20:02

evaluation question
 
Mediator = function() {
        
        var debug = function() {
            // console.log or air.trace as desired
        };
        
        var components = {};
        
        var broadcast = function(event, args, source) {
            var e = event || false;
            var a = args || [];
            if (!e)  return;
            //debug(["Mediator received", e, a].join(' '));
            for (var c in components) {
                if (typeof components[c]["on" + e] == "function") {
                    try {
                        var s = source || components[c];
                        components[c]["on" + e].apply(s, a); //??????????????
                    } catch (err) {
                        debug(["Mediator error.", e, a, s, err].join(' '));
                    }
                }
            }
        };
        
        var addComponent = function(name, component, replaceDuplicate) {
            if (name in components) {
                if (replaceDuplicate) {
                    removeComponent(name);
                } else {
                    throw new Error('Mediator name conflict: ' + name);
                }
            }
            components[name] = component;
        };
        
        var removeComponent = function(name) {
            if (name in components) {
                delete components[name];
            }
        };
        
        var getComponent = function(name) {
            return components[name] || false;
        };
        
        var contains = function(name) {
            return (name in components);
        };
        
        return {
            name      : "Mediator",
            broadcast : broadcast,
            add       : addComponent,
            rem       : removeComponent,
            get       : getComponent,
            has       : contains
        };
    }();
	
	Mediator.add('TestObject', function() {
        
        var someNumber = 0; // sample variable
        var someString = 'another sample variable';
        
        return {
            onInitialize: function() {
                // this.name is automatically assigned by the Mediator
                alert(this.name + " initialized.");
            },
            onFakeEvent: function() {
                someNumber++;
                alert("Handled " + someNumber + " times!");
            },
            onSetString: function(str) {
                someString = str;
                alert('Assigned ' + someString);
            }
        }
    }());

Mediator.broadcast("Initialize");



в строке components[c]["on" + e].apply(s, a)
во что "резолвиться" components[c]["on" + e]?

конкретно для этого примера вроде должно быть onInitialize.apply(TestObject) в этом случае получаем undefined

Спасибо

melky 12.05.2012 10:05

Цитата:

Сообщение от belbek
в строке components[c]["on" + e].apply(s, a)
во что "резолвиться" components[c]["on" + e]?

в функцию-обработчик. все они находятся в хеше components

Цитата:

Сообщение от belbek
конкретно для этого примера вроде должно быть onInitialize.apply(TestObject) в этом случае получаем undefined

тут получаем components["TestObject"], но не conmonents["onInitialize"]. в add должно идти : имя события, обработчик события, и флаг затирания старого результата. имя события идет в broadcast


Часовой пояс GMT +3, время: 12:26.