JS问题笔记——模拟Jq底层实现工厂模式

<script type="text/javascript">

(function (window,undefined){

    function _$(arguments)
    {

    }

    _$.prototype={
        constructor:_$,
        addEvent:function(){

                alert("addEvent");
                return this;
        },
        getStyle:function(){
                alert('getStyle');
                return this;
        }
    }
    window.$=_$;

    _$.onReady=function(fn){
        window.$=function(){
            return new _$(arguments);
        }

        fn();

    }

})(window)

$.onReady(function(){
    $("inq").addEvent().getStyle();
})

</script>


<script type="text/javascript">

(function (window,undefined){

    function _$(arguments)
    {

    }
    Function.prototype.method=function(methodName,fn)
    {
            this.prototype[methodName]=fn;
            return this;
    }

    _$.prototype={
        constructor:_$,
        addEvent:function(){

                alert("addEvent");
                return this;
        },
        getStyle:function(){
                alert('getStyle');
                return this;
        }
    }
    window.$=_$;

    _$.onReady=function(fn){
        window.$=function(){
            return new _$(arguments);
        }

        
        _$.method('addEvent',function(){
            alert("addEvent2");
                return this;
        }).method('getStyle',function(){
        alert("getStyle2");
                return this;
        });
fn();
    }

})(window)

$.onReady(function(){
    $("inq").addEvent().getStyle();
})
 //为什么要加_$.method()? 不加也行?
</script>

 

posted @ 2017-05-26 20:54  herozhou工巧  阅读(401)  评论(0编辑  收藏  举报