当前位置: 首页 > 图文教程 > 网络编程 > ASP > 初学js者对javascript面向对象的认识分析

ASP
IIS访问ASP页面时报错The requested resource is in use.的解决办法
错误类型:Provider (0x80004005)未指定的错误 的一个处理方法
关于asp+access的安全问题分析
把RS.GetRows看得更清楚
asp下UTF-8页面乱码的解决方法
解决使用良精企业建站7.0未注册问题
在ASP中连接MySQL数据库的方法,最好的通过ODBC方法
flash与js通讯方法
ASP操作Excel相关技术总结
解决用Access数据库建站维护不便的问题的方法
ASP实现GB2312字符与区位码的相互转换的代码
对于ASP编码问题的深入研究与最终解决方案
ASP生成UTF-8编码的代码
删除A表中在B表中不存在的数据
动网防恶意广告比较有效的办法附asp代码
asp磁盘缓存技术使用的代码
网页语言编码及asp乱码问题解决方案
FSO遍历目录实现全站插马的代码
关于无限分级(ASP+数据库+JS)的实现代码
如何写ASP入库小偷程序

ASP 中的 初学js者对javascript面向对象的认识分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 86 ::
收藏到网摘: n/a

初学js者对javascript面向对象的认识分析,需要学习的朋友可以参考下。
复制代码 代码如下:

var obj = document.getElementById("name");
function clickMe() {
alert(this.value);
this.value += "!!!!";
alert(this.value);
}
var ActionBinder = function() {//定义一个类
}
ActionBinder.prototype.registerDOM = function(doms) {
this.doms = doms;//注册doms
}
ActionBinder.prototype.registerAction = function(handlers) {
this.handlers = handlers;//注册一个动作
}
ActionBinder.prototype.bind = function() {
this.doms.onclick = this.handlers
}//注册doms的动作
var binder = new ActionBinder();//按照ActionBinder的方法新建一个类
binder.registerDOM(obj);
binder.registerAction(clickMe);
binder.bind();

先上一段用js写的面向对象的代码,先建立一个ActionBinder的类,写法上也类似于java;因为js是基于html的dom对象来操作html的内容,在类中定义一个注册dom的方法registerDOM,用prototype将该方法原型化,方便调用;另外再增加一个注册事件的方法registerAction,也用prototype方法原型化;最后再用一个原型化的动作bind将已注册的dom和已注册的事件绑定在了一起,并执行。
再上一段原始的js代码片段:
Code
复制代码 代码如下:

<body>
<script>
document.onload= function(){
var obj = document.getElementById("name");
obj.onclick = function(){alert(this.value);}
}
</script>
<input type="text" id="name" />
</body>

代码也实现了要的效果,对于一些简单的应用,上面那段效果能够满足,但对于比较复杂的一些程序,应用起来就比较麻烦,代码上写起来也较繁琐;如代码片段
Code
复制代码 代码如下:

<body>
<script>
document.onload= function(){
obj1 = document.getElementById("name1");
obj2 = document.getElementById("name2");
obj3 = document.getElementById("name3");
obj1.onclick = function(){alert(this.value);}
obj2.onclick = function(){alert(this.value);}
obj3.onclick = function(){alert(this.value);}
}
</script>
<input type="text" id="name1" value="111" />
<input type="text" id="name2" value="222" />
<input type="text" id="name3" value="333" />
</body>

或者
Code
复制代码 代码如下:

<body>
<script>
function clickMe(){alert(this.value);}
</script>
<input type="text" id="name1" value="111" onclick="return clickMe()" />
<input type="text" id="name2" value="222" onclick="return clickMe()" />
<input type="text" id="name3" value="333" onclick="return clickMe()" />
</body>

当然上面两段代码也有其他一些更简单的写法,总的来说还是出现很多冗余的代码。
用面向对象的方法写就比较灵活,如
Code
复制代码 代码如下:

<body>
<script>
window.onload = function() {
var objs = document.getElementsByTagName("input");
function clickMe() {
alert(this.value);
}
var ActionBinder = function() {//定义一个类
}
ActionBinder.prototype.registerDOM = function(doms) {
this.doms = doms;//注册doms
}
ActionBinder.prototype.registerAction = function(handlers) {
this.handlers = handlers;//注册一个动作
}
ActionBinder.prototype.bind = function() {
this.doms.onclick = this.handlers
}//注册doms的动作
for (var i=0;i<objs.length;i++ ){
var binder = new ActionBinder();//按照ActionBinder的方法新建一个类
binder.registerDOM(objs[i]);
binder.registerAction(clickMe);
binder.bind();
};
}
</script>
<input type="text" id="name" value="111"/>
<input type="text" id="name1" value="222"/>
<input type="text" id="name2" value="333"/>
</body>

这样就不会有冗余的代码,而且js逻辑上也比较清爽,对于多个事件的绑定还有待研究。