jQuery源碼分析以及從jQuery對象建立的角度理解extend方法的原理

來源:互聯網
上載者:User
1.1     建立屬於jQuery對象的外掛程式

前面我看到jQuery外掛程式的方式:通過$.extend方式可以定義屬於jQuery本身的全域性的外掛程式,為此我做了下面的測試,大家先看下面這段js代碼:

;(function($){    // 建立jQuery全域範圍的外掛程式    $.extend({        'wholeftn':function(){            console.log('你要用jQuery.wholeftn()方式調用,如果jQuery(XX).wholeftn()就會報錯');            },        'wholeattr':'全域jQuery屬性'    });    // 建立jQuery對象的外掛程式    $.fn.extend({        'partfrn':function(){            console.log('你要用jQuery(XX).wholeftn()方式調用,如果jQuery.wholeftn()就會報錯');            },        'partattr':'局部jQuery範圍'    });})(jQuery)

 

測試代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>jQuery.extend和jQuery.fn.extend區別</title></head><script type="text/javascript" src="js/jquery-1.7.1.js"></script><script type="text/javascript" src="js/jquery.extenddiff.js"></script><body></body></html><script type="text/javascript">$(document).ready(function(){    $.wholeftn();// 你要用jQuery.wholeftn()方式調用,如果jQuery(XX).wholeftn()就會報錯    console.log($.wholeattr);// 全域jQuery屬性        $('body').partfrn();//你要用jQuery(XX).wholeftn()方式調用,如果jQuery.wholeftn()就會報錯    console.log($('body').partattr);// 局部jQuery範圍        // 錯誤測試    console.log($('body').wholeattr);// undefined    console.log($.partattr);// undefined    //$('body').wholeftn();// $("body").wholeftn is not a function    $.partftn();// $.partftn is not a function});</script>

  我們發現$.extend是建立jQuery對象全域的方法和屬性,這很像java裡的靜態方法和靜態變數,而用$.fn.extend建立的是jQuery(XX)對象的方法,二者是有區別的:區別在於一個是屬於全域的一個是屬於對象的。我們平時經常使用的$.ajax就是全域方法而$(‘div’).html()就是屬於jQuery對象的方法,我們仔細瞧瞧jQuery手冊裡,jQuery幾乎所有的屬性和方法其實都可以按照屬於jQuery全域和屬於jQuery對象進行分類,因此我有個看法了:

  想理解jQuery架構的原理,讀懂它的原始碼,外掛程式技術是一個很好的切入點,整個jQuery架構大致就是分為三大部分:第一部分就是如何構建jQuery對象,第二部分是如何建立屬於jQuery全域的屬性和方法,第三部分就是如何建立屬於jQuery對象的屬性和方法了,而第一部分是jQuery架構的根本,後面兩部分就是jQuery架構的延伸了,只要理解了第一部分,包括如何構建jQuery全域外掛程式和jQuery對象外掛程式我們就可以真正理解jQuery架構設計原理了

  下面我就會好好分析下jQuery架構是如何構建jQuery對象的。 1.2     jQuery架構裡如何定義jQuery對象

jQuery對象的定義使用的是javascript最基礎的技術:對象的建立和對象的繼承技術,具體點就是建立一個javascript對象以及使用javascript裡的prototype技術,大家先看下面的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>javascript 物件導向</title></head><body></body></html><script type="text/javascript">// 定義一個Person類,也可以說是對象,javascript裡面對象和類是混合在一起的// 類或者說對象的定義又是建構函式和類的定義混合在一起的function Person(name,age,job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = function(){        console.log(this.name);        }}Person.prototype.commonType = 'Human';Person.prototype.commonFtn  = function(){    console.log('Name:' + this.name + ';Age:' + this.age + ';Job:' + this.job);    }Person.staticType = 'Animal';Person.staticFtn = function(){    console.log('All are Mammal');    }var per1 = new

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.