您好,欢迎访问一九零五行业门户网

jQuery的初始化与对象构建之浅析_jquery

小结一下:
1.整个类库定义在一匿名函数中,杜绝了全局变量的产生;
2.将undefined 作为缺失的参数传递,防止了undefined 变量的污染;
3.可以看出$(...) 实际上返回的是jquery.fn.init 对象的实例,随后将该对象的prototype 指向了jquery.prototype (语句jquery.fn.init.prototype = jquery.fn),因此产生的实例共享着jquery.prototype 里的方法和属性且实现了链式编程的操作;
4.最后通过window.jquery = window.$ = jquery 将jquery 与$ 导出为全局变量。
复制代码 代码如下:
(function(window, undefined) {
// define a local copy of jquery
var jquery = (function() {
var jquery = function(selector, context) {
// the jquery object is actually just the init constructor 'enhanced'
return new jquery.fn.init(selector, context/*, rootjquery*/);
};
// ...
jquery.fn = jquery.prototype = {
constructor : jquery,
init : function(selector, context, rootjquery) {
// ...
}
// ...
};
// give the init function the jquery prototype for later instantiation
jquery.fn.init.prototype = jquery.fn;
// ...
// expose jquery to the global object
return jquery;
})();
// ...
window.jquery = window.$ = jquery;
})(window);
其它类似信息

推荐信息