没有合适的资源?快使用搜索试试~ 我知道了~
初学js者对javascript面向对象的认识分析
0 下载量 168 浏览量
2021-01-20
08:09:40
上传
评论
收藏 48KB PDF 举报
温馨提示
![preview](https://dl-preview.csdnimg.cn/14877403/0001-c0ac535213ca821f64ca868b93a6f853_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
试读
2页
代码如下: 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 = funct
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/14877403/bg1.jpg)
初学初学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>
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38713393
- 粉丝: 8
- 资源: 878
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0