JS学习第9天——ES6中面向对象(类class、constructor构造函数、类的继承extends、super关键字、面向对象tab栏切换案例)

一、面向对象

两大编程思想:① 面向过程面向对象

1、面向过程

面向过程POP((Process Oriented programming):分析好了步骤,然后按照步骤解决问题

2、面向对象

面向对象OOP((Object Oriented Programming):以对象功能来划分问题,而不是步骤
面向对象特性
封装性继承性 多态性

3、两者对比

对比 面向对象 面向过程
优点 由于面对对象有封装、继承、多态性的特性,可以设计出低耦合的系统,使系统更加灵活、易维护、易复用、易扩展 性能比面向对象高,适合跟硬件联系很紧密的东西,比如单片机
缺点 性能比面向过程低 没有面对对象易于维护、易复用、易扩展

面向过程的方法写出来的程序像一份蛋炒饭,用面向对象写出来的程序像一份盖浇饭

二、ES6中的类和对象

1、面向对象的思维特点

① 抽取(抽象)对象共用的属性和行为封装成一个(模板)
② 对类进行实例化,获取类的对象

2、对象

在JS中,对象是一组无序的相关属性和方法的集合,所有的事物都是对象,比如字符串、数值、数组、函数等
对象是由属性和方法组成的,属性是事物的特征,方法是事物的行为

3、类class

在ES6中新增了类,通过class关键字声明一个类,之后以这个类来实例化对象
类抽取了对象的公共部分,通过类实例化一个具体的对象
创建类

class name {
    
    
	// 逻辑
}
// 实例化类
var xxx = new class();

注意:类必须使用 new 实例化对象

4、类constructor构造函数

constructor()方法是类的构造函数用于传递参数,返回实例对象

通过new关键字生成对象实例时,会自动调用constructor()方法。如果没有显示定义,类内部会自动帮我们创建一个constructor()
《1》语法

class Person {
    
    
	constructor(name,age) {
    
    
		this.name = name;
		this.age = age;
		say() {
    
    
			console.log(this.name + '你好');
		}
	}
}

//  创建实例
var ldh = new Person("刘德华",18);
console.log(ldh.name);
ldh.say();

注意:方法之间不能通过逗号隔开,同时方法也不需要添加function关键字

三、类的继承

子类可以继承父类的一些属性和方法

1、继承

extends关键字:表明子类继承父亲的属性和方法

class Father {
    
    
}
class Son extends Father {
    
     // 子类继承父类
}

2、super()关键字

super关键字用于访问和调用父类上的函数,可以调用父类的构造函数也可调用普通函数

// 父类
class Father {
    
    
  constructor(fatherName) {
    
    
    this.fatherName = fatherName;
  }
  say1() {
    
    
    console.log("我是父亲1");
  }
  say2() {
    
    
    console.log("我是父亲2");
  }
}
// 子类
class Son extends Father {
    
    
  // 子类可以继承父类
  constructor(fatherName, sonName) {
    
    
    super(fatherName);
    super.say1(); // 子类调用父类的构造函数
    this.sonName = sonName; // 子类定义自己独有的属性
  }
}
var lala = new Son("父亲", "儿子");
lala.say2(); // 子类调用父类的普通函数

注意: 子类在构造函数中使用super关键字时, 必须放到 this 前面 (必须先调用父类的构造方法,再使用子类构造方法)

3、注意点

① ES6中没有变量提升,所以必须先定义类,才能通过类实例化对象
② 类里面的共有属性和方法必须要加this使用
③ 类中this指向:constructor 里面的this指向实例对象, 方法里面的this 指向这个方法的调用者

四、面向对象案例

tab栏切换

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      * {
      
      
        margin: 0;
        padding: 0;
      }

      ul li {
      
      
        list-style: none;
      }
      .tabsbox {
      
      
        width: 900px;
        margin: 0 auto;
        height: 400px;
        border: 1px solid lightsalmon;
        position: relative;
      }

      nav ul {
      
      
        overflow: hidden;
      }

      nav ul li {
      
      
        position: relative;
        float: left;
        width: 100px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        border-right: 1px solid #ccc;
        position: relative;
      }

      nav ul li.liactive {
      
      
        background-color: pink;
        z-index: 9;
      }

      #tab input {
      
      
        width: 80%;
        height: 60%;
      }

      nav ul li a {
      
      
        position: absolute;

        top: 0px;
        right: 0;
        width: 10px;
        height: 10px;
        line-height: 10px;
        cursor: pointer;
        background-color: #ccc;
      }

      .tabadd {
      
      
        position: absolute;
        /* width: 100px; */
        top: 0;
        right: 0;
      }

      .tabadd span {
      
      
        display: block;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        border: 1px solid #ccc;
        float: right;
        margin: 10px;
        user-select: none;
      }

      .tabscon {
      
      
        width: 100%;
        height: 300px;
        position: absolute;
        padding: 30px;
        top: 50px;
        left: 0px;
        box-sizing: border-box;
        border-top: 1px solid #ccc;
      }

      .tabscon section,
      .tabscon section.conactive {
      
      
        display: none;
        width: 100%;
        height: 100%;
      }

      .tabscon section.conactive {
      
      
        display: block;
      }
    </style>
  </head>

  <body>
    <div class="tabsbox" id="tab">
      <!-- tab 标签 -->
      <nav class="fisrstnav">
        <ul>
          <li class="liactive"><span>测试1</span><a>×</a></li>
          <li><span>测试2</span><a>×</a></li>
          <li><span>测试3</span><a>×</a></li>
        </ul>
        <div class="tabadd">
          <span>+</span>
        </div>
      </nav>

      <!-- tab 内容 -->
      <div class="tabscon">
        <section class="conactive">测试1</section>
        <section>测试2</section>
        <section>测试3</section>
      </div>
    </div>
  </body>
  <script>
    var that;
    class Tab {
      
      
      constructor(id) {
      
      
        that = this;
        this.tab = document.querySelector(id);
        this.ul = this.tab.querySelector("ul");
        this.tabadd = this.tab.querySelector(".tabadd");
        this.tabscon = this.tab.querySelector(".tabscon");
        this.init();
      }
      // 初始化
      init() {
      
      
        // this指向实例对象
        this.updateTab();
        for (var i = 0; i < this.lis.length; i++) {
      
      
          this.lis[i].index = i;
          this.lis[i].onclick = this.toggleTab;
          this.as[i].index = i;
          this.as[i].onclick = this.removeTab;
          this.spans[i].ondblclick = this.editTab;
          this.sections[i].ondblclick = this.editTab;
        }
        this.tabadd.onclick = this.addTab;
      }

      // 更新数据
      updateTab() {
      
      
        this.lis = this.ul.querySelectorAll("li");
        this.sections = this.tabscon.querySelectorAll("section");
        this.as = this.ul.querySelectorAll("a");
        this.spans = this.ul.querySelectorAll("span");
      }
      //切换功能
      toggleTab() {
      
      
        // this指向li
        that.removeClass();
        this.className = "liactive";
        that.sections[this.index].className = "conactive";
      }

      // 添加功能
      addTab() {
      
      
        // this指向tabadd
        that.removeClass();
        var li = '<li class="liactive"><span>新增测试</span><a>×</a></li>';
        that.ul.insertAdjacentHTML("beforeend", li);
        var section = ' <section class="conactive">新增测试</section>';
        that.tabscon.insertAdjacentHTML("beforeend", section);
        that.init();
      }

      // 删除功能
      removeTab(e) {
      
      
        // this指向span
        e.stopPropagation();
        that.lis[this.index].remove();
        that.sections[this.index].remove();
        that.init();
        if (document.querySelector(".liactive")) return;
        that.lis[this.index - 1] && (that.lis[this.index - 1].className = "liactive");
      }

      //修改功能
      editTab() {
      
      
        // this指向span
        // 双击取消选中文字
        window.getSelection  ? window.getSelection().removeAllRanges() : document.selection.empty();
        var str = this.innerHTML;
        this.innerHTML = "<input type='text'>";
        var ipt = this.children[0];
        ipt.select();
        ipt.onblur = function () {
      
      
          ipt.value ? (this.parentNode.innerHTML = ipt.value) : (this.parentNode.innerHTML = str);
        };
      }

      // 移出样式
      removeClass() {
      
      
        for (var i = 0; i < this.lis.length; i++) {
      
      
          this.lis[i].className = "";
          this.sections[i].className = "";
        }
      }
    }
    new Tab("#tab");
  </script>
</html>

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Vest_er/article/details/129386607