(function(window){})(window)
为什么要传window给jquery当参数呢?
1.为了压缩有 引用
2.加速变量的寻找,当找window对象的时候,默认从本级开始寻找,一级级往上到最高层。
window.a==undefined
typeof window.a "undefined" 理论上两种都行,但是在老版本的IE中上种可能不行
传统的js的面向对象构建方式
function A(){}
A.prototype.init=function(){}
A.prototype.css=function(){}
使用:
var a=new A();
a.init();
a.css();
是否很麻烦
jQuery的做法:
function jQuery(){
return new jQuery.prototype.init();
}
jQuery.prototype.init=function(){}
jQuery.prototype.css=function(){}
jQuery.prototype.init.prototype=jQuery.prototype;//很巧妙,prototype首先是对象,也是型构造函数的原型
使用:
jQuery().css(); 直接省略了两步,被初始化了的
constructor属性,当创建一个js对象的时候,该对象自动有个 constructor属性,该属性就是构造方法
function A(){}
A.prototype.constructor=A;
当var a=new A()的时候,上面这句话自动会赋予的
可以 alert(a.constructor);试试
为什么在 jQuery中 jQuery.fn=jQuery.prototype={......,constructior:jQuery,....}
这一句呢,不是说会自动添加上的么?
首先
function A(){}
A.prototype.name='ww'
A.prototype.age='11'
var a=new A();
alert(a.constructor);//打印出function A(){}
然后 function A(){}
A.prototype={name:'ww',age:'11'}
var a=new A();
alert(a.constructor)//打印出 function Object(){[native object]}
这是因为 第一种 是附加属性 不会影响 构造对象constructor
第二种是 = ,赋予属性而 json格式的 是一个大对象,所以 constructor对象被赋予 object了
写了那句话之后是为了修正,修正为函数形式的。
$("li") 转成的格式为
this:{0:li,1:li,2:li,length}
$.parseHTML()用于实现以上功能转成以上string
$.merge() 合并数组,也可以合并json(但是这个json是key是0,1等的。。。)。 把数组转换成 json
var str="<li>1</li><li>2</li><li>3</li><script>alert(4)</script>"
$.parseHTML(str,document,true);//返回数组,每个对象都是一个标签对象['li','li','li','script']
var arr={
0:'a',
1:'b'
length:2}
vr arr2={'c','d'}
$.merge(arr,arr1) ->{0:'a',1:'b',2:'c',3:'d',length:4}形式的json格式了。
$.makeArray() 生成数组
var div=$("div")
$.makeArray(div)生成 div的数组
$.makeArray()
深拷贝与浅拷贝
var a={}
var b={name:{age:30}}
a.name.age=20
$.extend(a,b)
alert(b.name.age) 打印出20 浅拷贝
$.extend(true,a,b)
a.name.age=20;
allert(b.name.age) 打印出30深拷贝,不同对象