javascript单例模式的模块增强模式将函数类加入数组,再从数组中查找出进行调用

单例模式的增强模块
将类或函数类加入到数组中,然后从数组中查找函数名,如果存在即调出调用,
这种模块主要是管理应用程序级的信息和调用
比如你有很多的模块类,就可以加入到这个单例模块中,然后进行管理和调用

let application=function(){
    let components=new Array();
    components.push(new BaseComponent());
    let app=new BaseComponent();
    app.getComponentCount=function(){
        return components;
    };
    app.reisterComponent=function(component){
        if(typeof new component=="object")
        {
            components[component.name]=new component;
        }
    };
    app.getObject=function(name)
    {
        if(Object.keys(components).includes(name))
        {
            return components[name];
        }
    }
    return app;
}();

function BaseComponent(){}
function Person(){
    this.name='nicholas';
    this.getName=function(){
        return this.name;
    };
}
Person.prototype.bind=function(){
    console.log('bind');
};
application.reisterComponent(Person);
console.log(application.getComponentCount());
let person=application.getObject('Person');
person.bind();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值