//求大神能给我逐句翻译下。
// 以前自己写JS, 就是一个function一个function的, 它这种写法好奇怪。 function H$(i) {return document.getElementById(i)}
function H$$(c, p) {return p.getElementsByTagName(c)}
var slider = function () {
function init (o) {
this.id = o.id;
this.at = o.auto ? o.auto : 3;
this.o = 0;
this.pos();
}
init.prototype = {
pos : function () {
clearInterval(this.__b);
this.o = 0;
var el = H$(this.id), li = H$$('div', el), l = li.length;
var _t = li[l-1].offsetHeight;
var cl = li[l-1].cloneNode(true);
cl.style.opacity = 0; cl.style.filter = 'alpha(opacity=0)';
el.insertBefore(cl, el.firstChild);
el.style.top = -_t + 'px';
this.anim();
},
anim : function () {
var _this = this;
this.__a = setInterval(function(){_this.animH()}, 20);
},
animH : function () {
var _t = parseInt(H$(this.id).style.top), _this = this;
if (_t >= -1) {
clearInterval(this.__a);
H$(this.id).style.top = 0;
var list = H$$('div',H$(this.id));
H$(this.id).removeChild(list[list.length-1]);
this.__c = setInterval(function(){_this.animO()}, 20);
//this.auto();
}else {
var __t = Math.abs(_t) - Math.ceil(Math.abs(_t)*.07);
H$(this.id).style.top = -__t + 'px';
}
},
animO : function () {
this.o += 2;
if (this.o == 100) {
clearInterval(this.__c);
H$$('div',H$(this.id))[0].style.opacity = 1;
H$$('div',H$(this.id))[0].style.filter = 'alpha(opacity=100)';
this.auto();
}else {
H$$('div',H$(this.id))[0].style.opacity = this.o/100;
H$$('div',H$(this.id))[0].style.filter = 'alpha(opacity='+this.o+')';
}
},
auto : function () {
var _this = this;
this.__b = setInterval(function(){_this.pos()}, this.at*1000);
}
}
return init;
}();
http://kb.cnblogs.com/page/97011/
function H$(i) {return document.getElementById(i)}
function H$$(c, p) {return p.getElementsByTagName(c)}
var slider = function () {
function init (o) {//可以看做类的构造函数,用new init(o)的方式可以构造一个对象(o应该为Object并必须包含id属性,可能包含auto属性),包含下面定义的属性和prototype扩展的属性
this.id = o.id;
this.at = o.auto ? o.auto : 3;//如果o中有属性auto,则属性at初始为o.auto,否则为3;
this.o = 0;
this.pos();//调用pos方法,在下面prototype扩展的
}
init.prototype = {//为init类扩展成员
pos : function () {//为init类添加一个pos的方法,以下类似的不再注释
clearInterval(this.__b);
this.o = 0;//给自身属性o赋值
var el = H$(this.id), li = H$$('div', el), l = li.length;
var _t = li[l-1].offsetHeight;
var cl = li[l-1].cloneNode(true);
cl.style.opacity = 0; cl.style.filter = 'alpha(opacity=0)';
el.insertBefore(cl, el.firstChild);
el.style.top = -_t + 'px';
this.anim();
},
anim : function () {
var _this = this;
this.__a = setInterval(function(){_this.animH()}, 20);//为自身扩展成员_a,用于保存定时器
},
animH : function () {
var _t = parseInt(H$(this.id).style.top), _this = this;
if (_t >= -1) {
clearInterval(this.__a);//销毁定时器,就是this._a保存那个
H$(this.id).style.top = 0;
var list = H$$('div',H$(this.id));
H$(this.id).removeChild(list[list.length-1]);
this.__c = setInterval(function(){_this.animO()}, 20);//用成员_c保存定时器
//this.auto();
}else {
var __t = Math.abs(_t) - Math.ceil(Math.abs(_t)*.07);
H$(this.id).style.top = -__t + 'px';
}
},
animO : function () {
this.o += 2;
if (this.o == 100) {
clearInterval(this.__c);
H$$('div',H$(this.id))[0].style.opacity = 1;
H$$('div',H$(this.id))[0].style.filter = 'alpha(opacity=100)';
this.auto();
}else {
H$$('div',H$(this.id))[0].style.opacity = this.o/100;
H$$('div',H$(this.id))[0].style.filter = 'alpha(opacity='+this.o+')';
}
},
auto : function () {
var _this = this;
this.__b = setInterval(function(){_this.pos()}, this.at*1000);
}
}
return init;
}();我xxx,虽然每句我都看得懂,但是这么一大段,让我怎么看,注解不下去了,直接说原理吧
其实并没有什么特别怪异的写法,就是js弱类型的灵活性使然,
1.js的函数都可以用于构造对象(new xxx()的方式),当做构造函数的函数一般内部有this.aa=xx给对象添加成员比如
function cat(color)
{
this.color=color;
this.show=function(){alert(this.color);};
}
var acat=new cat("blue");
acat.show();
2.可以用cat.prototype给cat类增加方法
比如:
cat.prototype.eat=function(){alert(this.eat);};
这是再用new cat构造对象的话,就会多出一个eat方法
3.js可以直接给对象添加属性
比如 var obj=new Object();
alert(obj.a)//这儿会obj.a=undefind
obj.a="a";
alert(obj.a);//现在obj.a="a"了,所以js对象的属性不存在有没有定义,只是有没有赋值的区别4.还可以用配置对象的方式构造对象:
var obj={a:"a",b:function(){alert(this.a);}}
这句等价于
var obj=new Object()
obj.a="a";
obj.b=function(){alert(this.a);}上面的代码所有的写法都没有超出我上面列举的四条
init.prototype={
pos:function(){...}
}
我们就可以用init.pos()来调用