响亮的标题:一个万能的,保底的。面向过程改写成面向对象的方法

前提朗读:很多刚接触js面向对象的时候都不知道如何能快速的写出一个面向对象的程序,这个是必然的现象,不是每一位学js的一上来就会写面向对象的程序。小编这里介绍的方法属于一个面向过程到面向对象的过度期间,(这里要明白面向对象的一些常识:构造函数创建出对象,对象.属性,对象.方法)

一:改写前提步骤:

  1.前提:面向过程中的所有代码都在window.onload 里面
  2.面向过程里面所有的函数都领到外面去(不能有函数嵌套)
  3.提出共同的变量为全局变量(可以有全局变量 当有两个以上的函数都用到了共同的一个变量的话--把这个变量提出来变成全局变量)

小总结4:替换过程(注意this的使用)

面向过程 面向对象
onload(初始化整个程序) 构造函数(初始化对象)
全局变量 属性(对象.属性)     this.属性
函数  方法(构造函数.原型.方法)    构造函数.prototype.方法

** 备注重点:
  改错:this,事件,闭包,传参
  重点:通过闭包传递this

**小编提示:

  面向对象中70%以上都会用到this这个关键字,所以要想使用好面向对象,必须弄明白this

二:案例展示:(不注重要是布局)

1.面向过程案例展示

<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1'); //获取父级
var aBtn=oDiv.getElementsByTagName('input'); //获取oDiv下的所有的input
var aDiv=oDiv.getElementsByTagName('div'); //获取oDiv下的所有div
var i=0; //预先设置变量 i
//思路:清除所有的input元素上的class,并影藏对应的div。
//然后:添加当前点击的那个input的class。并显示对应的那个div for(i=0;i<aBtn.length;i++)
{
aBtn[i].index=i; //给每一个input元素新增加一个属性index
aBtn[i].onclick=function ()
{ //思路:先清除所有的input上面的class,并隐藏input对应的div
for(i=0;i<aBtn.length;i++)
{
aBtn[i].className='';
aDiv[i].style.display='none';
} //思路:然后给当前点击的那个input添加上class = active,并显示出input对应的那个div
this.className='active';
aDiv[this.index].style.display='block';
};
}
};
</script> <div id="div1">
<input class="active" type="button" value="教育" />
<input type="button" value="财经" />
<input type="button" value="aaa" />
<div style="display:block;">11111</div>
<div style="display:none">22222</div>
<div style="display:none">33333</div>
</div>

2.面向过程里面所有的函数都领到外面去(不能有函数嵌套)。提出共同的变量为全局变量

<script>
var aDiv = null; //声明全局变量
var aBtn = null; //声明全局变量 window.onload=function ()
{
var oDiv=document.getElementById('div1'); //获取父级
aBtn=oDiv.getElementsByTagName('input'); //获取oDiv下的所有的input
aDiv=oDiv.getElementsByTagName('div'); //获取oDiv下的所有div
var i=0; //预先设置变量 i
//思路:清除所有的input元素上的class,并影藏对应的div。
//然后:添加当前点击的那个input的class。并显示对应的那个div for(i=0;i<aBtn.length;i++)
{
aBtn[i].index=i; //给每一个input元素新增加一个属性index
aBtn[i].onclick=tab;
}
}; function tab(){
//思路:先清除所有的input上面的class,并隐藏input对应的div
for(i=0;i<aBtn.length;i++)
{
aBtn[i].className='';
aDiv[i].style.display='none';
} //思路:然后给当前点击的那个input添加上class = active,并显示出input对应的那个div
this.className='active';
aDiv[this.index].style.display='block';
};
</script> <div id="div1">
<input class="active" type="button" value="教育" />
<input type="button" value="财经" />
<input type="button" value="aaa" />
<div style="display:block;">11111</div>
<div style="display:none">22222</div>
<div style="display:none">33333</div>
</div>

3,this修改后

<script>
window.onload = function(){
var oTab = new TabSwitch('div1');
} function TabSwitch(id) //构造函数
{
var oDiv=document.getElementById(id); //获取父级
this.aBtn=oDiv.getElementsByTagName('input'); //获取oDiv下的所有的input
this.aDiv=oDiv.getElementsByTagName('div'); //获取oDiv下的所有div
var i=0; //预先设置变量 i
//思路:清除所有的input元素上的class,并影藏对应的div。
//然后:添加当前点击的那个input的class。并显示对应的那个div var _this = this; //this表示new出来的对象 for(i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].index=i; //给每一个input元素新增加一个属性index
this.aBtn[i].onclick=function(){
_this.tab(this); // ** _this表示通过构造函数new出来的对象。 this表示调用事件onclick的对象--> 按钮input
} }
}; TabSwitch.prototype.tab = function(oBtn){
//思路:先清除所有的input上面的class,并隐藏input对应的div
for(i=0;i<this.aBtn.length;i++) //this表示通过构造函数new出来的对象
{
this.aBtn[i].className='';
this.aDiv[i].style.display='none';
} //思路:然后给当前点击的那个input添加上class = active,并显示出input对应的那个div
oBtn.className='active';
this.aDiv[oBtn.index].style.display='block';
};
</script> <div id="div1">
<input class="active" type="button" value="教育" />
<input type="button" value="财经" />
<input type="button" value="aaa" />
<div style="display:block;">11111</div>
<div style="display:none">22222</div>
<div style="display:none">33333</div>
</div>

咱使用this来讲2个面向对象的小案例:this啥时候会出问题。

  1:在定时器里面

  2:在事件里面(通过闭包的方式把this传过去)

面向对象this案例一(正常现象):

<script>
function Aaa(){
this.a = 55;
console.log(this) //表示new出来的对象
} Aaa.prototype.show = function (){
alert(this.a)
console.log(this) //表示new出来的对象
} var obj1 = new Aaa();
obj1.show(); // 55 </script>

面向对象this案例二(定时器里面的this):

<script>
function Aaa(){
this.a = 55;
setInterval(this.show, 3000); //this为 window 证明了定时器里面的this不正常为window
console.log(this) //构造函数对象
}
Aaa.prototype.show = function (){
alert(this) //表示为 window
}
var obj1 = new Aaa();
</script> 改进方法 <script>
function Aaa(){
this.a = 55;
var _this = this ; //this为构造函数的对象,然后赋给一个变量 _this
setInterval(function(){
_this.show(); //_this为 构造函数的对象
}, 3000);
console.log(this) //构造函数对象
} Aaa.prototype.show = function (){
alert(this) //构造函数的对象
}
var obj1 = new Aaa(); </script>

面向对象this案例三(事件里面的this):

<script>
function Aaa(){
this.a = 55;
document.getElementById('odiv1').onclick = this.show;
} Aaa.prototype.show = function(){
alert(this.a); //打印出:undefone 因为:this为 input按钮,但是input按钮上根本没有a这个属性,所以提示为undefine
console.log(this); // this为 input按钮
} window.onload = function(){
new Aaa();
}
</script> 改进后 <script>
function Aaa(){
this.a = 55;
var _this = this; // _this = this = 构造函数的对象 document.getElementById('odiv1').onclick =function(){
_this.show(); //_this 表示为构造函数的对象
}
} Aaa.prototype.show = function(){
alert(this.a); //55
} window.onload = function(){
new Aaa();
}
</script>
<input id="odiv1" type="button" value="按钮">

js面向过程改写成面向对象--通用方法的更多相关文章

  1. MySQL原生API、MySQLi面向过程、MySQLi面向对象、PDO操作MySQL

    [转载]http://www.cnblogs.com/52fhy/p/5352304.html 本文将举详细例子向大家展示PHP是如何使用MySQL原生API.MySQLi面向过程.MySQLi面向对 ...

  2. C++基础——C面向过程与C++面向对象编程01_圆面积求解

    #include "iostream";//包含C++的头文件using namespace std;//使用命名空间std标准的命名空间(在这个命名空间中定义了很多标准定义)vo ...

  3. JS高级 - 面向对象3(面向过程改写面向对象)

    改写: 1.前提:所有东西都在 onload 里 2.改写:不能有函数嵌套,可以有全局变量 onload --> 构造函数 全局变量 --> 属性 函数 --> 方法 4.改错: t ...

  4. js绝对地址图片转换成base64的方法

    //将图片转换成base64 function getBase64Image(url, callback){ var canvas = document.createElement('canvas') ...

  5. js中字符串处理成数字的方法

    <script> var a="11.1111"; var b="12.2222"; //第一种方法:乘以1的方法 //alert(a*1+b*1) ...

  6. js面向过程-拖拽

    1.步骤分析: 1.1 获取id 1.2 当鼠标点击时执行的js 1.3当鼠标移动时执行的js 1.4当鼠标放开时执行的js 2.代码实现 <!DOCTYPE html> <html ...

  7. IDataReader转换成list通用方法

    public static IList<T> ReaderToList<T>(this IDataReader dr) { //DateTime dt = DateTime.N ...

  8. js面向过程-经典选项卡

    源代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  9. js 日期证有效性验的通用方法

    开发的理念是“为复用而开发,为使用而组装”,代码的复用度将是项目和产品的一个重要的技术指标. var DateTools={ isDate:function(str) { var result = s ...

随机推荐

  1. windows 系统中打开一个数字证书所经历的过程

         今天在使用Outlook express调试CSP程序时,发现数字证书总是加载不上(提示该数字证书已经被破坏),使用断点进去跟踪一下,发现在CSP程序中调用CPVerifySignature ...

  2. Javascript中函数的四种调用方式

    一.Javascript中函数的几个基本知识点: 1.函数的名字只是一个指向函数的指针,所以即使在不同的执行环境,即不同对象调用这个函数,这个函数指向的仍然是同一个函数. 2.函数中有两个特殊的内部属 ...

  3. IntelliJ IDEA常用快捷键

    双击shift 项目所有目录查找 ctrl+f 文件查找特定内容 ctrl+shift+f 项目查找包含特定内容的文件 ctrl+n 新建类 ctrl+shift+n 查找文件 ctrl+e  最近的 ...

  4. Django的列表反序

    Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...

  5. 关于uboot中tftp上传内存数据到tftp服务器

    uboot下的tftp下载功能是非常重要和常见的功能.但是偶尔有些特殊需求的人需要使用uboot的tftp具有上传功能.默认的uboot没有tftp上传功能,如果需要修改uboot代码.使用时键入第4 ...

  6. showdialog窗体不在任务栏显示的问题处理

    场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...

  7. [svn] linux命令——svn分支创建、合并

    一.创建分支 1,创建一个分支 svn copy svn://xx.com/repo/trunk svn://xx.com/repo/branches/TRY-something -m 'make b ...

  8. 把vector中的string对象导入到字符指针数组中

    #include <iostream>#include <string>#include <vector>//#include <cctype>#inc ...

  9. nyoj 284 坦克大战 (优先队列)

    题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...

  10. Html滚动文字

    <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" direction=" ...