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

ASP
asp查询xml的代码 不刷新页面查询的方法
ASP 快速执行动态网页
asp实现本周的一周时间列表的代码
asp get和post数据接收过滤
为google量身定做的sitemap生成代码asp版
适合所有网站的rss和xml聚合功能asp代码
ASP MSSQL存储过程的实现小例
动网论坛的asp 数据库连接代码
ASP+MSSQL2000 数据库被批量注入后的解决方法
asp 去掉html中的table正则代码函数
asp 过滤非法字符函数
asp检测是否为中文字符函数
ASP 包含文件中的路径问题和使用单一数据库连接文件的解决方案
asp HTTP 500错误 常见问题分析
asp 关键词高亮显示(不区分大小写)
一段ASP的HTTP_REFERER判断代码
asp datediff 时间相减
asp下去除超链接的函数
asp连接mssql2005的代码
asp access数据库并生成XML文件范例

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 66 ::
收藏到网摘: 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逻辑上也比较清爽,对于多个事件的绑定还有待研究。