/**
* 添加一个组件实例到大屏中
*
* @param {Component} component 组件实例
* @return {Screen}
*/
addComponent: function (component) {
var self = this;
// 绑定组件的所有事件
component.bind(function (event) {
var args = Array.prototype.slice.call(arguments, 1);
args.unshift(this);
args.unshift('component.' + event);
self.dispatch.apply(self, args);
});
// 添加到大屏中
component.render(self.container);
// 放入列表中
self.componentsById[component.id] = component;
return self;
}
遇到这种问题,你应该先了解一下
apply
的意义。它表示执行函数,并且以第一个参数作为 context,第二个参数作为函数的参数依次传入。所以在这个地方,因为组件本身是通用的,有哪些事件,需要传入哪些参数无法确定,所以只好使用
apply
的方式把所有事件全部传到处理函数中。