javascript设计模式-组合模式(6)
组合模式是一种专门为创建Web上的动态用户界面而量身制定的模式。使用这种模式可以用一条命令在多个对象上激发复杂的或递归的行为。这可以简化粘贴性代码,使其更容易维护,而那些复杂行为则被委托给各个对象 传统的层级业务场景 /* * 场景模拟: * -> 公司 * -> 财务部门 * -> 张一 * -> 张二 * -> 张三 * -> 销售部门 * -> 张四 * -> 张五 * -> 张六 * * 实际的任务具体是落实到人上去实施的 也就是说只有人才具有具体的方法实现 * */ var Org = function (name) { this .name = name ; this .depts = [] ; }; Org.prototype = { constructor:Org , addDepts: function (child) { this .depts.push(child); return this ; } , getDepts: function () { return this .depts; } }; var Dept = function (name) { this .name = name ; this .persons = [] ; }; Dept.prototype = { constructor:Dept , addPersons: function