来源:未知 时间:2020-09-06 09:12 作者:小飞侠 阅读:次
[导读] 近期在读jQuery的源码。 发现这里有个东西很难理解。 这里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。 来看下jQuery的源码是怎么样定义的: 代码如下: (function(window,undefi...
|
近期在读jQuery的源码。 代码如下: (function( window, undefined ) {
var jQuery = (function() {
// 构建jQuery对象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery对象原型
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;// 合并内容到第一个参数中,后续大部分功能都通过该函数扩展 jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上扩展静态方法
jQuery.extend({
// something to do
});// 到这里,jQuery对象构造完成,后边的代码都是对jQuery或jQuery对象的扩展 return jQuery; })(); window.jQuery = window.$ = jQuery; })(window); 这里我们可以看到: 代码如下: var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery 其实jQuery.fn.init()返回一个对象。那么jquery.fn.init()返回的又是什么呢?
代码如下:
jQuery.fn = jQuery.prototype =
{
constructor: jQuery,
init: function( selector, context, rootjQuery ) { // something to do }
};
代码如下:
代码如下: jQuery.fn.init.prototype = jQuery.fn;
代码如下: jQuery.extends() jQuery.fn.extends()
代码如下: jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com