首页 > 代码库 > JS问题笔记——模拟Jq底层实现工厂模式
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>
JS问题笔记——模拟Jq底层实现工厂模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。