分析版本为:1.7.2(Wed Mar 21 12:46:34 2012 -0700)
总入口,25行就是了:
// Define a local copy of jQuery
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
},(引用内容)
这个是jQuery的构造函数。继续引申:
话说jQuery.fn.init是什么东西,99行:
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
...(引用内容)
fn就是一个prototype。
初始化时主要赋值的成员变量:this[](默认数组对象),this.length(默认数组对象长度),context(上下文对象),this.selector(选择子对象)等。
<!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>
</head>
<body>
四大皆空
</body>
<script language="javascript">
var A= function(x) {
this.xInner=x; // 成员变量
var yInner=2; // 局部变量
}
A.fn = A.prototype = {
FunX:function(obj){
alert(obj.xInner)
}
}
A.fn.FunY = function(obj){
alert(obj.yInner);
}
var a =new A(5);
a.FunX(a); // 5
a.FunY(a); // undefined
</script>
</html>
模拟了fn的原理,同时对局部变量和成员变量进行阐述。