面向对象---使用面向对象写函数
2013-06-17 21:59
260 查看
避免this出现歧义
具体事例----选项卡切换的面向过程写法
把面向过程的写法改为----面向对象的写法
原则: 不能有函数套函数、但可以有全局变量
过程:
onload → 构造函数
全局变量 → 属性
函数 → 方法
改错: this、事件、闭包、传参
第一步-----不能有函数嵌套,把函数全部拎出来,为了函数起作用,需要把局部变量改为全局变量。
第二步-----把普通函数改为原型,window onload里面的内容改为构造函数,具体的实例全部改为this。
第三步----修改不正确的this,使用一个变量传递只带不同的this含义。今天刚看到一个this的指针问题,可能涉及到这个。附上网址http://www.w3cfuns.com/thread-5593260-1-1.html,有兴趣的可以看看
function Aaa() { var _this=this; this.a=12; //当前的对象的this setInterval(function (){ //有定时器的时候,会出现this的误解问题 _this.show(); //把this存为一个变量,传递进来,因为this代表的不一样,所以定时器里面的this需要是外层的this,也就是Aaa }, 1000); } Aaa.prototype.show=function () { alert(this.a); }; var obj=new Aaa(); //obj.show();
具体事例----选项卡切换的面向过程写法
<!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> <style> #div1 input {background:#CCC;} #div1 .active {background:yellow;} #div1 div {width:200px; height:200px; background:#CCC; display:none;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> window.onload=function () { var oDiv=document.getElementById('div1'); var aBtn=oDiv.getElementsByTagName('input'); var aDiv=oDiv.getElementsByTagName('div'); var i=0; for(i=0;i<aBtn.length;i++) { aBtn[i].index=i; aBtn[i].onclick=function () { for(i=0;i<aBtn.length;i++) { aBtn[i].className=''; aDiv[i].style.display='none'; } this.className='active'; aDiv[this.index].style.display='block'; }; } }; </script> </head> <body> <div id="div1"> <input class="active" type="button" value="教育" /> <input type="button" value="财经" /> <input type="button" value="aaa" /> <div style="display:block;">1asdfasdfds</div> <div>2xzcvxzcv</div> <div>5332342345</div> </div> </body> </html>
把面向过程的写法改为----面向对象的写法
原则: 不能有函数套函数、但可以有全局变量
过程:
onload → 构造函数
全局变量 → 属性
函数 → 方法
改错: this、事件、闭包、传参
第一步-----不能有函数嵌套,把函数全部拎出来,为了函数起作用,需要把局部变量改为全局变量。
var aBtn=null; var aDiv=null; //变为全局变量 window.onload=function () { var oDiv=document.getElementById('div1'); aBtn=oDiv.getElementsByTagName('input'); aDiv=oDiv.getElementsByTagName('div'); var i=0; for(i=0;i<aBtn.length;i++) { aBtn[i].index=i; aBtn[i].onclick=tab; } }; function tab() //单独的函数 { for(i=0;i<aBtn.length;i++) { aBtn[i].className=''; aDiv[i].style.display='none'; } this.className='active'; aDiv[this.index].style.display='block'; }
第二步-----把普通函数改为原型,window onload里面的内容改为构造函数,具体的实例全部改为this。
window.onload=function () { var oTab=new TabSwitch('div1'); }; function TabSwitch(id) { var oDiv=document.getElementById(id); this.aBtn=oDiv.getElementsByTagName('input'); this.aDiv=oDiv.getElementsByTagName('div'); var i=0; for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].index=i; this.aBtn[i].onclick=this.tab; } } TabSwitch.prototype.tab=function () { for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].className=''; this.aDiv[i].style.display='none'; } this.className='active'; this.aDiv[this.index].style.display='block'; }
第三步----修改不正确的this,使用一个变量传递只带不同的this含义。今天刚看到一个this的指针问题,可能涉及到这个。附上网址http://www.w3cfuns.com/thread-5593260-1-1.html,有兴趣的可以看看
window.onload=function () { var oTab=new TabSwitch('div1'); }; function TabSwitch(id) { var oDiv=document.getElementById(id); this.aBtn=oDiv.getElementsByTagName('input'); this.aDiv=oDiv.getElementsByTagName('div'); var i=0; var _this=this; for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].index=i; this.aBtn[i].onclick=function () { _this.tab(this); }; } } TabSwitch.prototype.tab=function (oBtn) { for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].className=''; this.aDiv[i].style.display='none'; } oBtn.className='active'; this.aDiv[oBtn.index].style.display='block'; };
相关文章推荐
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- 6 Java基础语法(二维数组,参数传递与数据加密问题)&面向对象(面向对象思想,类与对象及使用,对象内存图解)
- Lua-面向对象中函数使用时冒号(:)和点(.)的区别
- 面向对象的程序设计学习笔记-19-静态成员函数的使用
- 面向对象及组件开发(七)传统选项卡和面向对象选项卡编写
- java基础笔记一函数、数组、面向对象
- JavaScript 面向对象程序设计 变量,函数定义之不可不知
- 面向对象中(包装类的使用)
- 面向对象-简单 final 关键字的使用
- 面向对象的JavaScript-2.面向对象基础
- Effective Java(用函数对象表示策略、优先使用静态成员类)
- C#语言使用多态(接口与override) ——帮您剔除对面向对象多态性的疑惑
- 混合编译:在c中使用c++中的类对象和类成员函数
- JAVA基础day05 面向对象思想 成员变量和方法 封装 构造方法和this的使用
- 源码-JavaScript&jQuery交互式前端开发-第3章-函数、方法与对象-使用字面量语法创建对象
- 面向对象程序设计上机练习三(有默认参数的函数)
- C++中函数对象的使用
- 面向对象第八课,面向对象三大基本特征,继承,基本概念(重要)
- Javascript基础与面向对象基础~第八讲 从人类的起源谈面向对象
- 模块使用以及面向对象介绍