jQuery源代码学习jQuery对象构建

//源代码地址:https://github.com/zhwq/lh_js

1.准备

  1.1简单的闭包;将代码包含在匿名函数中,避免外部代码的干扰;同时传入闭包外变量,将闭包内的定义对象暴露处理

      

(function( window, undefined ){//闭包,防止undefined重新定义
//
匿名函数的返回值
var jQuery = ( function() {
//使用var方式 定义函数;//function vs var方式定义函数的区别
var jQuery = function( selector, context ) {
//code here
//return
} )();
//将jQuery,$附加给window
window.jQuery = window.$ = jQuery;
})(window);

    1.2函数

        1.函数也是对象;

        2.函数定义的常见方式(1.使用关键字function; 2.使用字面量方式 literal)

        3.函数的返回值(默认值为关键字undefined)

   1.3 简单面向对象知识

       1.对象有constructor属性,存储自己的构造器函数

       2.对象有原型属性,在firefox等浏览器扩展了属性__proto__指向对象的原型对象(IE系列中没有)

2.jQuery类库构建核心jQuery对象部分分析

<!DOCTYPE html>
<html>
<head>
<title>jquery structure</title>
<meta charset="utf-8"/>
</head>
<body>
<script>
(
function( window, undefined ){//闭包,防止undefined重新定义
var document = window.document;

//匿名函数的返回值
var jQuery = ( function() {
//使用var方式 定义函数;//function vs var方式定义函数的区别
var jQuery = function( selector, context ) {
return jQuery.fn.init( selector, context ); //自定义的函数返回值;默认值为undefined
};
//jQuery.__proto__//指向对象的原型对象;将jQuery的原型对性存在属性fn中中
jQuery.fn = jQuery.prototype = {/*primitive object 对象字面量*/
constructor: jQuery,
init:
function( selector, context, rootjQuery /**/) {

// something
if ( !selector ) {
return this;
}
// DOMElement
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
//others;
//...
}

};
jQuery.fn.init.prototype
= jQuery.fn;

//return
return jQuery;
} )();
//将jQuery,$附加给window
window.jQuery = window.$ = jQuery;
})(window);
</script>
</body>
</html>


未完待续...

原文地址:https://www.cnblogs.com/zhng/p/2353556.html