(1)
function a( ){
alert(1);
}
可以写成 var a=function( ){ alert(1) } ,然后var b=a;b( )
将有名函数处理为匿名函数,把对函数的调用抽取出来,还能将引用赋值。
这个时候如果a=null或者var a=function( ){alert(2)},改变a的赋值,再执行b( )的时候,发现a无论怎么变化,b执行的结果都是a以前执行的结果。
(2)
$()是$(document).ready( )的简写形式,其实就是要匹配到document这个根,然后调用它的ready( )方法。
(3)
第一步:
var jQuery=function( )
}
可将jQuery看成一个函数(构造函数),也可以看成一个类。js中本来就是没类的概念,用new Function( )的方式来创建实例。经常在jquery中看到的jQuery其实是个构造函数名称。
第二步:
var jQuery=function( )
jQuery.prototype={ // 经常看见构造函数加prototype可以指向原型对象,然后再在原型对象里头加属性,加方法
}
}
jQuery.prototype,构造函数原型就指向原型对象,所以要写成Json格式,用大括号。
第三步:
var $=jQuery=function( ){ // jQuery赋给更短的变量$
jQuery.fn=jQuery.prototype={ //prototype比较长,用fn替代
jquery:"1,3,3",
size:function( ){
return this.length;
}
}
}
(4)
function A( a,b ){
this.a=a;
this.b=b;
}
A.prototype={
move:function( ){
alert(1);
}
}
var aa=new A(2,3);
aa.move( ); // 重置prototype在前,不需要重新指定构造函数,正常弹出5
而如果:
function A( a,b ){
this.a=a;
this.b=b;
}
var aa=new A(2,3); // 如果实例化在前,重置prototype在后的话,还要重新指定构造函数
A.prototype={
move:function( ){
alert(1);
}
}
加上 A.prototype.constructor=A;
aa.move( );
// 正常弹出5
A.prototype={ }这样的形式就是重置原型对象的行为,所以一般不用这样的形式,而用
A.prototype.move=function( ){ }这样的形式,不会破坏已有结构,仅仅是加该要加的函数就可以。
(5)
function A( ){
return new A( );
}
A( )会出现死循环。创建自己,创建自己,一直弄下去。。
(6)
(7)
(8)