var jQuery = (function() { var jQuery = function( selector, context ) { return new jQuery.fn.init( selector, context, rootjQuery ); } // jQuery對(duì)象原型 jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { //do something } }; jQuery.fn.init.prototype = jQuery.fn;jquery源碼中,通過jQuery.fn.init.prototype = jQuery.fn;這一句將jQuery.prototype的方法掛載到new出來的新的jquery對(duì)象上,但是當(dāng)我使用jQuery.extend({ sayhello:function(){ console.log("Hello,This is jQuery Library"); }})extend來定義一個(gè)新方法時(shí),這個(gè)方法按理來說應(yīng)該是1.既綁定到了jQuery.prototype上 原因:jQuery.extend = jQuery.fn.extend=function(){}2.也綁定到了new出來的新jquery對(duì)象上,原因:jQuery.fn.init.prototype = jQuery.fn但是!這種情況下我$("div").sayhello()卻報(bào)錯(cuò)Uncaught TypeError: $(...).sayhello is not a function(…)而$.sayhello();卻可以成功Hello,This is jQuery Library很顯然這個(gè)sayhello成為了jquery原型的方法,但是并沒有掛載到new出來的新的jquery對(duì)象上請(qǐng)問這種情況應(yīng)該怎么理解呢?
一個(gè)關(guān)于jquery源碼的問題: jQuery.fn.init.prototype
千萬里不及你
2018-08-10 10:09:04