函数化构造解决初始化与指向问题
回顾前几日动画模块的 JavaScript 构建方式:
具体代码如下:
var photo=function(){
var index=0,a,b,c,d;
return {
show:function(){},
auto:function(){}
}
}
var aa=photo();
// 核心逻辑是通过 return 语句返回一系列方法。
// 1:无法在初始化阶段直接执行 auto 方法。
// 2: 在初始化过程中,难以将 this 正确指向 aa 实例。
// 上述两个缺陷会导致使用上的不便。
1:我不希望采用如下写法:
var aa=photo("id");
aa.auto()//额外调用一行代码,显得不够优雅。
理想状态下,我在
var aa=photo("id")时即可告知程序是否启用自动播放。
2:若页面中存在多个动画实例。
例如:
var aa=photo("id1");
aa.auto()
var bb=photo("id2");
bb.auto()
此时它们各自拥有控制动画的 A 标签,需确保互不干扰。(这涉及私有变量的作用域及 this 指向问题);
声明:网上已有多种解决上述问题的方案。此处仅分享我已理清的思路,高手请多包涵。
不错,又是在公车上,我解决了这个问题。《javascript语言精粹》第52页 5.4函数化
我们来看一下这个 函数化构造器的源代码:
//加粗表示强调
//这个方法是 《javascript语言精粹》第52页 5.4函数化 上的。
var constructor = function (spec,my){
var that,其他的私有实例变量;
my = my || {};
把共享的变量和函数添加到my中
that = 一个新对象
添加给that 的特权方法
return that;
}
看下面的方法:
var photo = function(spec){
var _this={},index,a,c,d;
//这里可以初始化用户控制的a标签
//比如这样
a.onmouseover=function(){
_this.go();//可以调用哦
}
_this.show=function(){};
_this.auto=function(){};
_this.go=function(){};
// 这里可以直接调用刚才申明好的方法
_this.auto()//可以直接调用
return _this;
}
var bb=photo({index:1;});
var aa=photo({index:2});
//上面创建了 bb aa 两个不同的动画,不会互相影响哦。
// 如果我对javascript语言精粹 的函数化 理解有问题。还请指教...
最后这个动画就比较完美了。可是私有变量太多了。如果可以设置默认值,可以使用者选择性的传进来。会比较好
所以可以添加下面这个函数:(这是很多人都使用的啦)
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
// 看到 Extend 大家都知道 他是做什么用的了。
最后贴出我今天写的这个 图片轮换的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>javascript 图片轮换</title> </head> <body> <style type="text/css"> body,ul,li{ margin:0; padding:0;} #div{width:610px;height:210px;overflow:hidden;} #photo li{height:210px; overflow:hidden; background:#fff url(001.gif) no-repeat center center;} #photo2 li{height:210px; overflow:hidden; background:#fff url(001.gif) no-repeat center center;} .number-warp{position:absolute;margin-top:-50px;width:600px; text-align:right;} .number-warp a{border:1px solid #CCC; width:30px; height:30px; display:inline-block; height:100%;margin-left:5px; text-align:center; text- decoration:none;} .sel{background:#CCC;} </style> <ul id="photo" style="margin-top:0px;"> <li><img src="https://img.jbzj.com/demoimg/201005/r_img1.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img2.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img3.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img4.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img5.jpg" /></li> </ul> 1 2 3 4 5 <ul id="photo2" style="margin-top:0px;"> <li><img src="https://img.jbzj.com/demoimg/201005/r_img1.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img2.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img3.jpg" /></li> <li><img src="https://img.jbzj.com/demoimg/201005/r_img4.jpg" /></li> </ul> 1 2 3 4 <script type="text/javascript"> var Extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; } var photo=function(spec){ var $=function(id){ return document.getElementById(id); }, _this={}, a=document.getElementById(spec["number"]).getElementsByTagName("A"), my={ $:"", change:-210, d:50, num:3, au:true, index:0,x:null,v:null }, tween=function(t,b,c,d){ return -c*(t/=d)*(t-2)+b; }, autoo=function(){ _this.show( my.index+1 > my.num ? 0 : my.index+1); }; Extend(my,spec)//用他更好一点 for(var i=0 ;i </body> </html> [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]