面向过程 | 面向对象 | |
---|---|---|
优势 | 性能比面向对象高,适合跟硬件联系很紧密的东西,例如单片机就采用的面向过程编程。 | 易维护、易复用、易扩展,因为面向对象有封装、继承、多态性的特性,能够设计出低耦合的系统,使系统 更加灵活、更加易于维护 |
缺点 | 不易维护、不易复用、不易扩展 | 性能比面向过程低 |
对象是由属性和方法组成的:是一个无序键值对的集合,指的是一个具体的事物html
//如下代码是对对象的复习 //字面量建立对象 var ldh = { name: '刘德华', age: 18 } console.log(ldh); //构造函数建立对象 function Star(name, age) { this.name = name; this.age = age; } var ldh = new Star('刘德华', 18)//实例化对象 console.log(ldh);
如上两行代码运行结果为:编程
//步骤1 使用class关键字 class name { // class body } //步骤2使用定义的类建立实例 注意new关键字 var xx = new name();
// 1. 建立类 class 建立一个 明星类 class Star { // 类的共有属性放到 constructor 里面 constructor(name, age) { this.name = name; this.age = age; } } // 2. 利用类建立对象 new var ldh = new Star('刘德华', 18); console.log(ldh);
以上代码运行结果:dom
经过结果咱们能够看出,运行结果和使用构造函数方式同样函数
// 1. 建立类 class 建立一个类 class Star { // 类的共有属性放到 constructor 里面 constructor是 构造器或者构造函数 constructor(uname, age) { this.uname = uname; this.age = age; }//------------------------------------------->注意,方法与方法之间不须要添加逗号 sing(song) { console.log(this.uname + '唱' + song); } } // 2. 利用类建立对象 new var ldh = new Star('刘德华', 18); console.log(ldh); // Star {uname: "刘德华", age: 18} ldh.sing('冰雨'); // 刘德华唱冰雨
以上代码运行结果:性能
注意哟:测试
// 父类 class Father{ } // 子类继承父类 class Son extends Father { }
class Father { constructor(surname) { this.surname= surname; } say() { console.log('你的姓是' + this.surname); } } class Son extends Father{ // 这样子类就继承了父类的属性和方法 } var damao= new Son('刘'); damao.say(); //结果为 你的姓是刘
以上代码运行结果:this
子类使用super关键字访问父类的方法spa
//定义了父类 class Father { constructor(x, y) { this.x = x; this.y = y; } sum() { console.log(this.x + this.y); } } //子元素继承父类 class Son extends Father { constructor(x, y) { super(x, y); //使用super调用了父类中的构造函数 } } var son = new Son(1, 2); son.sum(); //结果为3
注意:设计
继承中,若是实例化子类输出一个方法,先看子类有没有这个方法,若是有就先执行子类的3d
继承中,若是子类里面没有,就去查找父类有没有这个方法,若是有,就执行父类的这个方法(就近原则)
若是子类想要继承父类的方法,同时在本身内部扩展本身的方法,利用super 调用父类的构造函数,super 必须在子类this以前调用
// 父类有加法方法 class Father { constructor(x, y) { this.x = x; this.y = y; } sum() { console.log(this.x + this.y); } } // 子类继承父类加法方法 同时 扩展减法方法 class Son extends Father { constructor(x, y) { // 利用super 调用父类的构造函数 super 必须在子类this以前调用,放到this以后会报错 super(x, y); this.x = x; this.y = y; } subtract() { console.log(this.x - this.y); } } var son = new Son(5, 3); son.subtract(); //2 son.sum();//8
以上代码运行结果为:
时刻注意this的指向问题,类里面的共有的属性和方法必定要加this使用.
在 ES6 中类没有变量提高,因此必须先定义类,才能经过类实例化对象
为获取到的标题绑定点击事件,展现对应的内容区域,存储对应的索引
this.lis[i].index = i; this.lis[i].onclick = this.toggleTab;
使用排他,实现只有一个元素的显示
toggleTab() { //将全部的标题与内容类样式所有移除 for (var i = 0; i < this.lis.length; i++) { this.lis[i].className = ''; this.sections[i].className = ''; } //为当前的标题添加激活样式 this.className = 'liactive'; //为当前的内容添加激活样式 that.sections[this.index].className = 'conactive'; }
为添加按钮+ 绑定点击事件
this.add.onclick = this.addTab;
实现标题与内容的添加,作好排他处理
addTab() { that.clearClass(); // (1) 建立li元素和section元素 var random = Math.random(); var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"> </span></li>'; var section = '<section class="conactive">测试 ' + random + '</section>'; // (2) 把这两个元素追加到对应的父元素里面 that.ul.insertAdjacentHTML('beforeend', li); that.fsection.insertAdjacentHTML('beforeend', section); that.init(); }
为元素的删除按钮x绑定点击事件
this.remove[i].onclick = this.removeTab;
获取到点击的删除按钮的所在的父元素的全部,删除对应的标题与内容
removeTab(e) { e.stopPropagation(); // 阻止冒泡 防止触发li 的切换点击事件 var index = this.parentNode.index; console.log(index); // 根据索引号删除对应的li 和section remove()方法能够直接删除指定的元素 that.lis[index].remove(); that.sections[index].remove(); that.init(); // 当咱们删除的不是选中状态的li 的时候,原来的选中状态li保持不变 if (document.querySelector('.liactive')) return; // 当咱们删除了选中状态的这个li 的时候, 让它的前一个li 处于选定状态 index--; // 手动调用咱们的点击事件 不须要鼠标触发 that.lis[index] && that.lis[index].click(); }
为元素(标题与内容)绑定双击事件
this.spans[i].ondblclick = this.editTab; this.sections[i].ondblclick = this.editTab;
在双击事件处理文本选中状态,修改内部DOM节点,实现新旧value值的传递
editTab() { var str = this.innerHTML; // 双击禁止选定文字 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); // alert(11); this.innerHTML = '<input type="text" />'; var input = this.children[0]; input.value = str; input.select(); // 文本框里面的文字处于选定状态 // 当咱们离开文本框就把文本框里面的值给span input.onblur = function() { this.parentNode.innerHTML = this.value; }; // 按下回车也能够把文本框里面的值给span input.onkeyup = function(e) { if (e.keyCode === 13) { // 手动调用表单失去焦点事件 不须要鼠标离开操做 this.blur(); } } }