最近幾天一直在研究jquery原始碼,由於水平太低看得昏頭轉向。本來理解的也不是很深刻,下面就用自己的想法來說下jquery是如何定義建構函式初始化的。如果有什麼不對的地方,希望個位高手指出。
一般寫建構函式如下
functionaaa(){}
aaa.prototype.init = function
(){};
aaa.prototype.css = function
(){};
var a1 = new
aaa();
a1.init();
//初始化
a1.css();
jquery寫法如下
functionjquery();
jquery.prototype =,
css :
function
(){}
}jquery.prototype.init.prototype =jquery.prototype;
jquery().css();
jquery() -> new jquery.prototype.init();
jquery.prototype.init.prototype = jquery.prototype;
把jquery的原型指向了,自己的init方法(看作建構函式)的原型上。 (不知道怎麼說,反正是這個意思,希望高手指出。)注:這裡加上 constructor 屬性主要起到修正作用。
示例
functionaaa(){}
var a1 = new
aaa();
//建構函式本身, 自動生成 aaa.prototype.constructor = aaa;
alert(a1.constructor); //
aaa//
在aaa原型上加2個屬性
aaa.prototype.name = 'hello';
aaa.prototype.age = 30;
alert(a1.constructor);
//還是aaa,不會變化
//如果重構了aaa的原型,即覆蓋
aaa.prototype =;
var a1 = new
aaa();
alert(a1.constructor);
//如果不加constructor : aaa 指向改變了
jQuery原始碼學習
jquery框架學習 1 定義變數和函式 2 給jq物件,新增一些方法和屬性 3 extend jq的繼承方法 4 jq.extend 擴充套件jq的一些工具方法 5 jq複雜的選擇功能 6 callbacks 回函物件,函式的統一管理 7 deferred 延遲物件,對非同步的乙個統一管理 8 s...
ArrayList建構函式原始碼
shared empty array instance used for empty instances.private static final object empty elementdata shared empty array instance used for default sized ...
jQuery原始碼學習筆記
整個jquery是乙個自呼叫的匿名函式 1 function global,factory 9return factory w 10 11 else 14 typeof window undefined window this,function window,noglobal 自呼叫函式大家都不陌生...