jQuery无new创建对象原理
2019-12-02 14:25 盛世游侠 阅读(269) 评论(0) 编辑 收藏 举报// jQuery 无new 创建对象套路 (function(g,undefined){ var foo = function(){ return new foo.fn.init(); }; foo.prototype = { saySth: function(){ console.log("say something"); }, init: function(){ console.log("inited"); return this; } }; foo.fn = foo.prototype; foo.fn.init.prototype = foo.fn; // 关键点 window.foo = foo; })(window); var o = foo(); o.saySth(); // 输出say something