最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-27 20:58:46
文档

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

jQuery的初始化与对象构建之浅析_jquery:小结一下: 1.整个类库定义在一匿名函数中,杜绝了全局变量的产生; 2.将undefined 作为缺失的参数传递,防止了undefined 变量的污染; 3.可以看出$(...) 实际上返回的是jQuery.fn.init 对象的实例,随后将该对象的prototype 指向了jQuery.
推荐度:
导读jQuery的初始化与对象构建之浅析_jquery:小结一下: 1.整个类库定义在一匿名函数中,杜绝了全局变量的产生; 2.将undefined 作为缺失的参数传递,防止了undefined 变量的污染; 3.可以看出$(...) 实际上返回的是jQuery.fn.init 对象的实例,随后将该对象的prototype 指向了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);

文档

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

jQuery的初始化与对象构建之浅析_jquery:小结一下: 1.整个类库定义在一匿名函数中,杜绝了全局变量的产生; 2.将undefined 作为缺失的参数传递,防止了undefined 变量的污染; 3.可以看出$(...) 实际上返回的是jQuery.fn.init 对象的实例,随后将该对象的prototype 指向了jQuery.
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top