亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個關于jquery源碼的問題: jQuery.fn.init.prototype

一個關于jquery源碼的問題: jQuery.fn.init.prototype

千萬里不及你 2018-08-10 10:09:04
 var jQuery = (function() {       var jQuery = function( selector, context ) {           return new jQuery.fn.init( selector, context, rootjQuery );       }       // jQuery對象原型       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對象上,但是當我使用jQuery.extend({  sayhello:function(){      console.log("Hello,This is jQuery Library");    }})extend來定義一個新方法時,這個方法按理來說應該是1.既綁定到了jQuery.prototype上 原因:jQuery.extend = jQuery.fn.extend=function(){}2.也綁定到了new出來的新jquery對象上,原因:jQuery.fn.init.prototype = jQuery.fn但是!這種情況下我$("div").sayhello()卻報錯Uncaught TypeError: $(...).sayhello is not a function(…)而$.sayhello();卻可以成功Hello,This is jQuery Library很顯然這個sayhello成為了jquery原型的方法,但是并沒有掛載到new出來的新的jquery對象上請問這種情況應該怎么理解呢?
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

jQuery.extend = jQuery.fn.extend=function(){}這一句只是表明 jQuery 和 jQuery的原型都指定了同一個繼承方法,并不代表你調用了jQuery.extend同時調用了jQuery.fn.extend

你用的jQuery.extend只是繼承到了jQuery這個全局函數上,只能$.sayhello();調用。


查看完整回答
反對 回復 2018-09-24
  • 1 回答
  • 0 關注
  • 737 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號