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

上面的空函數(shù)就是所謂的構(gòu)造函數(shù),構(gòu)造函數(shù)在面向?qū)ο笳Z言中是類的一個基本方法。
巧妙2:擴展原型
何為原型對象?我給出一篇博文大家可以去了解一下http://www.dbjr.com.cn/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ù),你也可以把它當(dāng)成類(呵呵,本身就是類)。
復(fù)制代碼 代碼如下:
(function(){
var jQuery = function() {
// 函數(shù)體
}
window.jQuery = window.$ = jQuery;
})();
console.log(jQuery);

上面的空函數(shù)就是所謂的構(gòu)造函數(shù),構(gòu)造函數(shù)在面向?qū)ο笳Z言中是類的一個基本方法。
巧妙2:擴展原型
何為原型對象?我給出一篇博文大家可以去了解一下http://www.dbjr.com.cn/article/32857.htm。
javascript為所有函數(shù)綁定一個prototype屬性,由這個屬性指向一個原型對象。我們在原型對象中定義類的繼承屬性和方法等。
原型對象是javascript實現(xiàn)繼承的基本機制。
復(fù)制代碼 代碼如下:
(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)用。
復(fù)制代碼 代碼如下:
(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();
復(fù)制代碼 代碼如下:
(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ù)體直接返回類的實例。
復(fù)制代碼 代碼如下:
(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作用域是什么?
復(fù)制代碼 代碼如下:
(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初始化類型。
復(fù)制代碼 代碼如下:
(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)造器的原型對象。
復(fù)制代碼 代碼如下:
jQuery.fn.init.prototype = jQuery.fn;
全部代碼:
復(fù)制代碼 代碼如下:
(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源碼學(xué)習(xí)javascript(三)
- jQuery中removeClass()方法用法實例
- 使用JS實現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能
- jQuery源碼解讀之removeClass()方法分析
相關(guān)文章
javaScript和jQuery自動加載簡單代碼實現(xiàn)方法
給大家介紹一下利用javaScript和jQuery實現(xiàn)自動加載最簡單的代碼寫法。2017-11-11幾種二級聯(lián)動案例(jQuery\Array\Ajax php)
這篇文章主要為大家詳細(xì)介紹了幾種二級聯(lián)動案例(jQuery\Array\Ajax php),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08jQuery插件實現(xiàn)的日歷功能示例【附源碼下載】
這篇文章主要介紹了jQuery插件實現(xiàn)的日歷功能,結(jié)合完整實例形式分析了jQuery datepicker插件實現(xiàn)日歷功能的相關(guān)操作技巧,需要的朋友可以參考下2018-09-09jQuery動畫顯示和隱藏效果實例演示(附demo源碼下載)
這篇文章主要介紹了jQuery動畫顯示和隱藏效果實現(xiàn)方法,并附帶了demo源碼供讀者下載參考,涉及jQuery操作圖片的顯示,隱藏及淡入淡出等效果,需要的朋友可以參考下2015-12-12jQuery基于BootStrap樣式實現(xiàn)無限極地區(qū)聯(lián)動
這篇文章主要介紹了jQuery基于BootStrap樣式實現(xiàn)無限極地區(qū)聯(lián)動的相關(guān)資料,需要的朋友可以參考下2016-08-08