jQuery的初始化與對(duì)象構(gòu)建之淺析
更新時(shí)間:2011年04月12日 00:14:37 作者:
之前本人的工作和學(xué)習(xí)多以原生js 為主,對(duì)jQuery 一直都不是很了解,但jQuery 作為當(dāng)今最優(yōu)秀的js 類(lèi)庫(kù)之一,必須是要花時(shí)間好好學(xué)習(xí)下的,今天正好蛋疼,讀了里面一些代碼
小結(jié)一下:
1.整個(gè)類(lèi)庫(kù)定義在一匿名函數(shù)中,杜絕了全局變量的產(chǎn)生;
2.將undefined 作為缺失的參數(shù)傳遞,防止了undefined 變量的污染;
3.可以看出$(...) 實(shí)際上返回的是jQuery.fn.init 對(duì)象的實(shí)例,隨后將該對(duì)象的prototype 指向了jQuery.prototype (語(yǔ)句jQuery.fn.init.prototype = jQuery.fn),因此產(chǎn)生的實(shí)例共享著jQuery.prototype 里的方法和屬性且實(shí)現(xiàn)了鏈?zhǔn)骄幊痰牟僮鳎?
4.最后通過(guò)window.jQuery = window.$ = jQuery 將jQuery 與$ 導(dǎo)出為全局變量。
(function(window, undefined) {
// Define a local copy of jQuery
var jQuery = (function() {
var jQuery = function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context/*, rootjQuery*/);
};
// ...
jQuery.fn = jQuery.prototype = {
constructor : jQuery,
init : function(selector, context, rootjQuery) {
// ...
}
// ...
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
// ...
// Expose jQuery to the global object
return jQuery;
})();
// ...
window.jQuery = window.$ = jQuery;
})(window);
1.整個(gè)類(lèi)庫(kù)定義在一匿名函數(shù)中,杜絕了全局變量的產(chǎn)生;
2.將undefined 作為缺失的參數(shù)傳遞,防止了undefined 變量的污染;
3.可以看出$(...) 實(shí)際上返回的是jQuery.fn.init 對(duì)象的實(shí)例,隨后將該對(duì)象的prototype 指向了jQuery.prototype (語(yǔ)句jQuery.fn.init.prototype = jQuery.fn),因此產(chǎn)生的實(shí)例共享著jQuery.prototype 里的方法和屬性且實(shí)現(xiàn)了鏈?zhǔn)骄幊痰牟僮鳎?
4.最后通過(guò)window.jQuery = window.$ = jQuery 將jQuery 與$ 導(dǎo)出為全局變量。
復(fù)制代碼 代碼如下:
(function(window, undefined) {
// Define a local copy of jQuery
var jQuery = (function() {
var jQuery = function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context/*, rootjQuery*/);
};
// ...
jQuery.fn = jQuery.prototype = {
constructor : jQuery,
init : function(selector, context, rootjQuery) {
// ...
}
// ...
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
// ...
// Expose jQuery to the global object
return jQuery;
})();
// ...
window.jQuery = window.$ = jQuery;
})(window);
您可能感興趣的文章:
- JavaScript/jQuery、HTML、CSS 構(gòu)建 Web IM 遠(yuǎn)程及時(shí)聊天通信程序
- 使用jQuery.wechat構(gòu)建微信WEB應(yīng)用
- jQuery學(xué)習(xí)筆記之jQuery構(gòu)建函數(shù)的7種方法
- Jquery 快速構(gòu)建可拖曳的購(gòu)物車(chē)DragDrop
- 精選的10款用于構(gòu)建良好易用性網(wǎng)站的jQuery插件
- 讀jQuery之三(構(gòu)建選擇器)
- JQuery 構(gòu)建客戶(hù)/服務(wù)分離的鏈接模型中Table分頁(yè)代碼效率初探
- JQuery 構(gòu)建客戶(hù)/服務(wù)分離的鏈接模型中Table中的排序分析
- 實(shí)例詳解jQuery的無(wú)new構(gòu)建
- 深入理解jQuery()方法的構(gòu)建原理
相關(guān)文章
jQuery幫助之篩選查找 children([expr])
取得一個(gè)包含匹配的元素集合中每一個(gè)元素的所有子元素的元素集合。2011-01-01
jQuery設(shè)置和移除文本框默認(rèn)值的方法
這篇文章主要介紹了jQuery設(shè)置和移除文本框默認(rèn)值的方法,實(shí)例分析了jQuery操作文本框的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery 可以拖動(dòng)的div實(shí)現(xiàn)代碼 腳本之家修正版
最近研究了一下jQuery,覺(jué)得真的是一個(gè)很不錯(cuò)的js庫(kù),其他的不說(shuō),關(guān)鍵是有翔實(shí)的文檔,這點(diǎn)是非常關(guān)鍵的。2009-06-06
jquery實(shí)現(xiàn)點(diǎn)擊其他區(qū)域時(shí)隱藏下拉div和遮罩層的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)點(diǎn)擊其他區(qū)域時(shí)隱藏下拉div和遮罩層的方法,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)改變頁(yè)面元素樣式的功能,需要的朋友可以參考下2015-12-12
使用jquery 簡(jiǎn)單實(shí)現(xiàn)下拉菜單
這里給大家展示了一例使用jQuery實(shí)現(xiàn)下拉菜單效果,代碼非常簡(jiǎn)潔,推薦給小伙伴們2015-01-01
使用jQuery實(shí)現(xiàn)的網(wǎng)頁(yè)版的個(gè)人簡(jiǎn)歷(可換膚)
點(diǎn)擊姓名會(huì)顯示她的基本詳細(xì)信息,點(diǎn)擊切換皮膚,會(huì)更改皮膚和字體大小感興趣的朋友可以參考下本文如何使用jQuery實(shí)現(xiàn)的網(wǎng)頁(yè)版的個(gè)人簡(jiǎn)歷2013-04-04
jQuery簡(jiǎn)單實(shí)現(xiàn)MD5加密的方法
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)MD5加密的方法,基于jquery.md5.js插件實(shí)現(xiàn)md5加密功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2017-03-03

