jquery源码分析(2)(2.0版本)

1.参数为什么传window和undefined:

(function(window,undefined){
})(window)

1.(1)根据作用域链的原理,传入window参数之后,能提高查找速度。(2).便于压缩。
2.传入undefined,防止undefined在外部被修改。

2.变量rootjQuery,core_strundefined
866行:rootjQuery = jQuery(document);
30行: core_strundefined = typeof undefined,
window.aundefined
typeof window.a
’undefined’
在ie678xml节点下判断window.a==undefined是会出错的。

3.$(),jQuery()调用方法的原理:
调用构造函数的方法:

function Test(){
}
Test.prototype.init = funciton(){
}
Test.prototype.add = function(){
}
var test1 = new Test()
test1.init()
test1.add()

使用构造函数的调用形式每次都要创建一个构造函数的实例。
jQuery中:

function jQuery(){
	return new jQuery.prototype.init()
}
jQuery.prototype.init = funciton(){}
jQuery.prototype.add = function(){}

调用jQuery时返回的是jQuery.prototype.init构造函数的实例,但是诸如jQuery.prototype.add此类方法又怎么拿得到呢?
在283行:jQuery.fn.init.prototype = jQuery.fn;
在96行:jQuery.fn = jQuery.prototype
这样jQuery的原型就赋给了jQuery.fn.init的原型,调用jQuery时,return出去的构造函数实例就可以引用jQuery原型上的方法和属性。
8752行:window.jQuery = window.$ = jQuery;//直接通过$()就可以调用jQuery方法了。

		(function(window,undefined){
			jQuery = function(select){
				return new jQuery.prototype.init(select)
			}
			jQuery.prototype.init = function(select){
				return document.getElementById(select)
			}
			jQuery.prototype.test = function(){
				alert('success')
			}
			jQuery.prototype.init.prototype = jQuery.prototype
			window.jQuery = window.$ = jQuery
		})(window)
		*$('dv').onclick = function(){
			$().test()
		}*

这样就可以实现一些简单的封装了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值