...这是一个拖动层,可放大缩小的程序
window.onload=function ()
{
.....
new PerfectDraging//new出来的是一个匿名函数,其实就是执行构造函数初始化了一下,可以这么理解吧?
(
oH2Title,//第一个参数
function ()
{return {x:oMsgbox.offsetLeft, y:oMsgbox.offsetTop};},//第二个参数,这里的作用其实就是返回一个对象,这个对象有两个属性,xy不是匿名函数内部的吗?可以这么理解吗?
function (x, y)//第三个参数,这个xy是从哪来的值?糊涂
{
var iSTop=document.body.scrollTop || document.documentElement.scrollTop;
if(x<0){x=0;}
else if(x+oMsgbox.offsetWidth>document.documentElement.clientWidth)
{x=document.body.clientWidth-oMsgbox.offsetWidth;}
if(y<iSTop)
{y=iSTop;}
else if(y+oMsgbox.offsetHeight>document.documentElement.clientHeight+iSTop)
{y=document.documentElement.clientHeight-oMsgbox.offsetHeight+iSTop;}
oMsgbox.style.left=x+'px';
oMsgbox.style.top=y+'px';
}//第二个参数的xy,和第三个参数的xy是同一个?第二个参数的return起什么作用?怎么理解?
);
//上面不就是这样new PerfectDraging(para1,para2,para3);这里隐藏着什么语法?或者什么机制?不懂了,请指教
.......
};
.......
function PerfectDraging(oElement, fnGetPos, fnOnDrag)
{
var obj=this;//这样的目的是什么?this到底是指什么?不是构造函数PerfectDraging?
this.oElement=oElement;
this.fnGetPos=fnGetPos;
this.fnOnDrag=fnOnDrag;
this.__oStartOffset__={x:0, y:0};//
this.fnOnMouseUp=function (ev)
{obj.stopDrag(window.event || ev);//this?如果这有this,是指fnOnMouseUp?};
//上面直接this不行吗?fnOnMouseUp可以理解为一个方法,也是一个函数,甚至也是一个对象,没错吧,糊涂了!
.......
}
PerfectDraging.prototype.startDrag=function (oEvent)//这里startDrag是PerfectDraging的原型,那么所有的new PerfectDraging出来的对象就有startDrag这个方法
{
var oPos=this.fnGetPos();//这个this是指谁?构造函数PerfectDraging?不是startDrag?越来越糊涂了,这里面到底隐藏着什么东西?
var x=oEvent.clientX;
..
this.__oStartOffset__.x=x-oPos.x;
..
if(this.oElement.setCapture)
{
this.oElement.setCapture();
this.oElement.onmouseup=this.fnOnMouseUp;
this.oElement.onmousemove=this.fnOnMouseMove;
}
else
{..}
};
PerfectDraging.prototype.stopDrag=function (oEvent)
{.....};
PerfectDraging.prototype.doDrag=function (oEvent)
{.....};
window.onload=function ()
{
.....
new PerfectDraging//new出来的是一个匿名函数,其实就是执行构造函数初始化了一下,可以这么理解吧?
(
oH2Title,//第一个参数
function ()
{return {x:oMsgbox.offsetLeft, y:oMsgbox.offsetTop};},//第二个参数,这里的作用其实就是返回一个对象,这个对象有两个属性,xy不是匿名函数内部的吗?可以这么理解吗?
function (x, y)//第三个参数,这个xy是从哪来的值?糊涂
{
var iSTop=document.body.scrollTop || document.documentElement.scrollTop;
if(x<0){x=0;}
else if(x+oMsgbox.offsetWidth>document.documentElement.clientWidth)
{x=document.body.clientWidth-oMsgbox.offsetWidth;}
if(y<iSTop)
{y=iSTop;}
else if(y+oMsgbox.offsetHeight>document.documentElement.clientHeight+iSTop)
{y=document.documentElement.clientHeight-oMsgbox.offsetHeight+iSTop;}
oMsgbox.style.left=x+'px';
oMsgbox.style.top=y+'px';
}//第二个参数的xy,和第三个参数的xy是同一个?第二个参数的return起什么作用?怎么理解?
);
//上面不就是这样new PerfectDraging(para1,para2,para3);这里隐藏着什么语法?或者什么机制?不懂了,请指教
.......
};
.......
function PerfectDraging(oElement, fnGetPos, fnOnDrag)
{
var obj=this;//这样的目的是什么?this到底是指什么?不是构造函数PerfectDraging?
this.oElement=oElement;
this.fnGetPos=fnGetPos;
this.fnOnDrag=fnOnDrag;
this.__oStartOffset__={x:0, y:0};//
this.fnOnMouseUp=function (ev)
{obj.stopDrag(window.event || ev);//this?如果这有this,是指fnOnMouseUp?};
//上面直接this不行吗?fnOnMouseUp可以理解为一个方法,也是一个函数,甚至也是一个对象,没错吧,糊涂了!
.......
}
PerfectDraging.prototype.startDrag=function (oEvent)//这里startDrag是PerfectDraging的原型,那么所有的new PerfectDraging出来的对象就有startDrag这个方法
{
var oPos=this.fnGetPos();//这个this是指谁?构造函数PerfectDraging?不是startDrag?越来越糊涂了,这里面到底隐藏着什么东西?
var x=oEvent.clientX;
..
this.__oStartOffset__.x=x-oPos.x;
..
if(this.oElement.setCapture)
{
this.oElement.setCapture();
this.oElement.onmouseup=this.fnOnMouseUp;
this.oElement.onmousemove=this.fnOnMouseMove;
}
else
{..}
};
PerfectDraging.prototype.stopDrag=function (oEvent)
{.....};
PerfectDraging.prototype.doDrag=function (oEvent)
{.....};
var obj = new Object();
obj.x = 1;
obj.y = 2;调用PerfectDraging()函数时,需要传递3个参数,后两个参数都需要接受function类型的数据。
function作为参数传递时,并非表示此function会立即执行,而是可能在之后才被调用,因此“这个xy”是在此function被调用之时传进去的,PerfectDraging()函数的后两个参数都是function类型,可以被称为“回调函数”,回调函数可以理解为是在适当的时候由系统调用的,并由系统传入参数。由上两段叙述可知,你所提到的“第二个参数的xy”和“第三个参数的xy”是毫无关系的。
第二个参数(fuction)中的xy是对象字面量的一部分;第三个参数中的xy,是第三个参数(即这个function)的两个形参。
第二个参数的return,作用如同一个普通function的returne,因为第二个参数本身就是一个function。JS的function可以嵌套,外层functoin作用域内的变量在内层function中也可以使用。
而this关键字在某个作用域内只能代表当前作用域内的this对象,如果希望引用外层作用域的this对象,可以在外层先用自定义变量保存其引用,这样就可以在内层作用域访问外层的“this”了。通过“构造函数”的prototype定义的方法,调用此方法时,其内部的“this”指向“构造函数”创建的那个对象。
纯理论比较晦涩,请其它童鞋补充代码实例吧。
window.onload=function ()
{
.....
oMsgbox=document.getElementById('msgbox');
new PerfectDraging//这里不就是调用PerfectDraging?
(
oH2Title,
function ()//这里我可以理解为第二个参数,是返回一个对象带有两个属性
{
return {x:oMsgbox.offsetLeft, y:oMsgbox.offsetTop};
},
“function作为参数传递时,并非表示此function会立即执行,而是可能在之后才被调用,因此“这个xy”是在此function被调用之时传进去的,”?这怎么理解,这new PerfectDraging不调用?
function (x, y)//这里呢?x,y呢?如果不是同一个,谁来赋值?
{
var iSTop=document.body.scrollTop || document.documentElement.scrollTop;
if(x<0)
{
x=0;
}
else if(x+oMsgbox.offsetWidth>document.documentElement.clientWidth)
{
x=document.body.clientWidth-oMsgbox.offsetWidth;
}
if(y<iSTop)
{
y=iSTop;
}
else if(y+oMsgbox.offsetHeight>document.documentElement.clientHeight+iSTop)
{
y=document.documentElement.clientHeight-oMsgbox.offsetHeight+iSTop;
}
oMsgbox.style.left=x+'px';
oMsgbox.style.top=y+'px';
}
);
......
PerfectDraging.prototype.startDrag=function (oEvent)
{
var oPos=this.fnGetPos();
//var x=oEvent.pageX || oEvent.x;
//var y=oEvent.pageY || oEvent.y;
var x=oEvent.clientX;
var y=oEvent.clientY;
this.__oStartOffset__.x=x-oPos.x;
this.__oStartOffset__.y=y-oPos.y;
if(this.oElement.setCapture)
{
this.oElement.setCapture();
this.oElement.onmouseup=this.fnOnMouseUp;
this.oElement.onmousemove=this.fnOnMouseMove;
}
else
{
document.addEventListener("mouseup", this.fnOnMouseUp, true);
document.addEventListener("mousemove", this.fnOnMouseMove, true);
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
};
.......
function test(callback) {
callback(3, 2);
}
// 两个回调函数
function plus(x, y) {
alert(x + y);
}
function minus(x, y) {
alert(x + y);
}// 将回调函数作为参数传给test。此时回调函数并未立即被调用,而是在test函数内部传参并调用
test(plus);
test(minus);
// 因此,我们可以通过传入不同的回调函数,让test具有不同的行为
// 可接收function作为参数的函数
function test(callback) {
callback(3, 2);
}
// 两个回调函数
function plus(x, y) {
alert(x + y);
}
function minus(x, y) {
alert(x - y);
}// 将回调函数作为参数传给test。此时回调函数并未立即被调用,而是在test函数内部传参并调用
test(plus);
test(minus);
// 因此,我们可以通过传入不同的回调函数,让test具有不同的行为