通過jQuery源碼學習javascript(二)
更新時間:2012年12月27日 16:01:41 作者:
昨天寫了篇通過jQuery源碼學習javascript(一),里面有一個定義對象C的方法,我早期也沒有太注意這個方面的技術(shù)細節(jié)。后來我查了一下資料,發(fā)現(xiàn)里面有很多巧的地方。今天與大家分享
巧妙1:函數(shù)
在javascript代碼中函數(shù)是個不可多得的人才。
♥ 它可以歸置代碼段,封裝相對獨立的功能。
♥ 它也可以實現(xiàn)類,注入OOP思想。
jQuery就是一個函數(shù),你也可以把它當成類(呵呵,本身就是類)。
(function(){
var jQuery = function() {
// 函數(shù)體
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery);

上面的空函數(shù)就是所謂的構(gòu)造函數(shù),構(gòu)造函數(shù)在面向?qū)ο笳Z言中是類的一個基本方法。
巧妙2:擴展原型
何為原型對象?我給出一篇博文大家可以去了解一下http://m.fzitv.net/article/32857.htm。
javascript為所有函數(shù)綁定一個prototype屬性,由這個屬性指向一個原型對象。我們在原型對象中定義類的繼承屬性和方法等。
原型對象是javascript實現(xiàn)繼承的基本機制。
(function(){
var jQuery = function() {
// 函數(shù)體
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
(new jQuery()).test();
巧妙3:使用工廠方法來創(chuàng)建一個實例
上面的方法必須使用下面的方法才能進行調(diào)用,這樣就會產(chǎn)生很多對象,從而浪費內(nèi)存消耗。
(new jQuery()).test();
jQuery源碼使用了很柔和的方法,也是大家比較熟悉的工廠方法,進行調(diào)用。
(function(){
var jQuery = function() {
// 函數(shù)體
return jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
return this;
},
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
jQuery().test();
(function(){
var jQuery = function() {
return this;
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery());
輸出結(jié)果
假想2:讓jQuery函數(shù)體直接返回類的實例。
(function(){
var jQuery = function() {
return new jQuery();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery());
輸出結(jié)果
巧妙4:分隔作用域
思考1:init()方法返回的this作用域是什么?
(function(){
var jQuery = function() {
// 函數(shù)體
return jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果

init()方法中的this作用域:this關(guān)鍵字引用了init()函數(shù)作用域所在的對象,同時也能夠訪問上一級對象jQuery.fn對象的作用?!@種思路會破壞作用域的獨立性,對于jQuery框架來說,很可能造成消極影響。
思考2:怎么把init()中的this從jQuery.fn對象中分隔出來?——實例化init初始化類型。
(function(){
var jQuery = function() {
// 函數(shù)體
return new jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果

通過實例化init()初始化類型,限定了init()方法里的this,只在init()函數(shù)內(nèi)活動,不讓它超出范圍。
巧妙5:原型傳遞
思考1:在巧妙4中,我們把init()中的this從jquery.fn對象中分隔出來。那我們?nèi)绾文茏龅奖WC“巧妙4”的基礎(chǔ)上,還能訪問jQuery原型對象呢?——原型傳遞。
讓jQuery的原型對象覆蓋init()構(gòu)造器的原型對象。
jQuery.fn.init.prototype = jQuery.fn;
全部代碼:
(function(){
var jQuery = function() {
// 函數(shù)體
return new jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果
把init()對象的prototype指針指向jQuery.fn。——這樣init()里的this繼承了jQuery.fn原型對象定義的方法和屬性。
總結(jié)
感謝博友的留言,尤其是puni ,給我介紹了一本不錯的書。如果大家能補充一下,那就再好不過了。
在javascript代碼中函數(shù)是個不可多得的人才。
♥ 它可以歸置代碼段,封裝相對獨立的功能。
♥ 它也可以實現(xiàn)類,注入OOP思想。
jQuery就是一個函數(shù),你也可以把它當成類(呵呵,本身就是類)。
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery);

上面的空函數(shù)就是所謂的構(gòu)造函數(shù),構(gòu)造函數(shù)在面向?qū)ο笳Z言中是類的一個基本方法。
巧妙2:擴展原型
何為原型對象?我給出一篇博文大家可以去了解一下http://m.fzitv.net/article/32857.htm。
javascript為所有函數(shù)綁定一個prototype屬性,由這個屬性指向一個原型對象。我們在原型對象中定義類的繼承屬性和方法等。
原型對象是javascript實現(xiàn)繼承的基本機制。
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
(new jQuery()).test();
巧妙3:使用工廠方法來創(chuàng)建一個實例
上面的方法必須使用下面的方法才能進行調(diào)用,這樣就會產(chǎn)生很多對象,從而浪費內(nèi)存消耗。
(new jQuery()).test();
jQuery源碼使用了很柔和的方法,也是大家比較熟悉的工廠方法,進行調(diào)用。
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
return jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
return this;
},
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
jQuery().test();

復制代碼 代碼如下:
(function(){
var jQuery = function() {
return this;
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery());
輸出結(jié)果

假想2:讓jQuery函數(shù)體直接返回類的實例。
復制代碼 代碼如下:
(function(){
var jQuery = function() {
return new jQuery();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
test: function() {
console.log('test');
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery());
輸出結(jié)果

巧妙4:分隔作用域
思考1:init()方法返回的this作用域是什么?
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
return jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果

init()方法中的this作用域:this關(guān)鍵字引用了init()函數(shù)作用域所在的對象,同時也能夠訪問上一級對象jQuery.fn對象的作用?!@種思路會破壞作用域的獨立性,對于jQuery框架來說,很可能造成消極影響。
思考2:怎么把init()中的this從jQuery.fn對象中分隔出來?——實例化init初始化類型。
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
return new jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果

通過實例化init()初始化類型,限定了init()方法里的this,只在init()函數(shù)內(nèi)活動,不讓它超出范圍。
巧妙5:原型傳遞
思考1:在巧妙4中,我們把init()中的this從jquery.fn對象中分隔出來。那我們?nèi)绾文茏龅奖WC“巧妙4”的基礎(chǔ)上,還能訪問jQuery原型對象呢?——原型傳遞。
讓jQuery的原型對象覆蓋init()構(gòu)造器的原型對象。
復制代碼 代碼如下:
jQuery.fn.init.prototype = jQuery.fn;
全部代碼:
復制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
return new jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
// 擴展原型對象
jquery: "1.8.3",
init: function() {
this.init_jquery = '2.0';
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery().jquery);
console.log(jQuery().init_jquery);
輸出結(jié)果

把init()對象的prototype指針指向jQuery.fn。——這樣init()里的this繼承了jQuery.fn原型對象定義的方法和屬性。
總結(jié)
感謝博友的留言,尤其是puni ,給我介紹了一本不錯的書。如果大家能補充一下,那就再好不過了。
您可能感興趣的文章:
- jQuery源碼解讀之a(chǎn)ddClass()方法分析
- jQuery源碼解讀之hasClass()方法分析
- jQuery源碼解讀之removeAttr()方法分析
- jQuery源碼分析之jQuery.fn.each與jQuery.each用法
- 從JQuery源碼分析JavaScript函數(shù)的apply方法與call方法
- jQuery源碼分析之jQuery中的循環(huán)技巧詳解
- 通過jQuery源碼學習javascript(三)
- jQuery中removeClass()方法用法實例
- 使用JS實現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能
- jQuery源碼解讀之removeClass()方法分析
相關(guān)文章
jQuery實現(xiàn)鼠標經(jīng)過時出現(xiàn)隱藏層文字鏈接的方法
這篇文章主要介紹了jQuery實現(xiàn)鼠標經(jīng)過時出現(xiàn)隱藏層文字鏈接的方法,涉及jQuery鼠標hover事件的響應(yīng)及頁面元素的動態(tài)處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
jQuery中實現(xiàn)prop()函數(shù)控制多選框(全選,反選)
下面小編就為大家?guī)硪黄猨Query中實現(xiàn)prop()函數(shù)控制多選框(全選,反選)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
為JQuery EasyUI 表單組件增加焦點切換功能的方法
下面小編就為大家?guī)硪黄獮镴Query EasyUI 表單組件增加焦點切換功能的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
基于jQuery實現(xiàn)的Ajax 驗證用戶名是否存在的實現(xiàn)代碼
基于jQuery實現(xiàn)的Ajax 驗證用戶名是否存在的實現(xiàn)代碼,需要的朋友可以參考下。2011-04-04
jQuery的3種請求方式$.post,$.get,$.getJSON
這篇文章主要介紹了jQuery的3種請求方式$.post,$.get,$.getJSON,需要的朋友可以參考下2014-03-03
jQuery Select(單選) 模擬插件 V1.3.62 改進版
改進jQuery Select(單選) 模擬插件 V1.3.6,增加mouseover事件2010-07-07

