js es6系列教程 - 新的类语法实战选项卡(详解)

作者:jingxian 时间:2024-11-12 17:09:41 

其实es6的面向对象很多原理和机制还是ES5的,只不过把语法改成类似php和java老牌后端语言中的面向对象语法.

一、用es6封装一个基本的类


class Person{
  constructor( uName ){
   this.userName = uName;
  }
  sayName(){
   return this.userName;
  }
 }

是不是很向php和java中的类, 其实本质还是原型链,我们往下看就知道了

首先说下语法规则:

class Person中的Person就是类名,可以自定义

constructor就是构造函数,这个是关键字,当实例化对象的时候,这个构造函数会被自动调用


let oP = new Person( 'ghostwu' );
 console.log( oP.sayName() ); //ghostwu

console.log( oP instanceof Person ); //true
 console.log( oP instanceof Object ); //true

console.log( typeof Person ); //function
 console.log( typeof Person.prototype.sayName ); //function
 console.log( oP.__proto__ === Person.prototype ); //true
 console.log( 'sayName' in oP ); //true
 console.log( Person.prototype );

第1行和第2行实例化和调用方法还是跟es5一样

第4行和第5行判断对象是否是类(Person)和Object的实例, 结果跟es5一样, 这个时候,我们肯定会想到Person的本质是否就是一个函数呢

第7行完全验证了我们的想法,类Person本质就是一个函数

第8行可以看到sayName这个函数其实还是加在Person的原型对象上

第9行还是验证了es5的原型链特点:对象的隐式原型指向构造函数的原型对象

第10行验证oP对象通过原型链查找到sayName方法

这种类的语法,被叫做语法糖,本质还是原型链

二、利用基本的类用法,封装一个加法运算


class Operator{
  constructor( n1 = 1, n2 = 2 ){
   this.num1 = n1;
   this.num2 = n2;
  }
  add( n1 = 10, n2 = 20 ){
   let num1 = n1 || this.num1, num2 = n2 || this.num2;
   return num1 + num2;
  }
 }
 var oper = new Operator();
 console.log( oper.add( 100, 200 ) );

三、利用基本的类语法,封装经典的选项卡

css代码:


#tab div {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  display: none;
 }

#tab div:nth-of-type(1) {
  display: block;
 }

.active {
  background: yellow;
 }

html代码:


<div id="tab">
 <input type="button" value="点我1" data-target="#div1" class="active">
 <input type="button" value="点我2" data-target="#div2">
 <input type="button" value="点我3" data-target="#div3">
 <input type="button" value="点我4" data-target="#div4">
 <div id="div1">1</div>
 <div id="div2">2</div>
 <div id="div3">3</div>
 <div id="div4">4</div>
</div>

javascript代码:


window.onload = () => {
  class Tab {
   constructor( context ) {
    let cxt = context || document;
    this.aInput = cxt.querySelectorAll( "input" );
    this.aDiv = cxt.querySelectorAll( "div" );
   }
   bindEvent(){
    let targetId = null;
    this.aInput.forEach(( ele, index )=>{
     ele.addEventListener( "click", ()=>{
      targetId = ele.dataset.target;
      this.switchTab( ele, targetId );
     });
    });
   }
   switchTab( curBtn, curId ){
    let oDiv = document.querySelector( curId );
    this.aDiv.forEach(( ele, index )=>{
     ele.style.display = 'none';
     this.aInput[index].className = '';
    });
    curBtn.className = 'active';
    oDiv.style.display = 'block';
   }
  }
  new Tab( document.querySelector( "#tab" ) ).bindEvent();
 }

来源:http://www.cnblogs.com/ghostwu/archive/2017/09/01/7465066.html

标签:js,es6,类语法
0
投稿

猜你喜欢

  • mysql 备份与迁移 数据同步方法

    2024-01-13 14:19:26
  • ASP:一个网站空间多个域名访问

    2008-11-21 17:03:00
  • PHP实现PDF转图片的详细过程(使用imagick)

    2023-09-07 13:27:34
  • Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案

    2024-05-02 17:09:28
  • SQL命令优化需要记住的9点事项

    2024-01-16 22:34:05
  • Python+PyQT5实现手绘图片生成器

    2022-03-11 11:57:21
  • python gdal安装与简单使用

    2022-06-19 00:03:25
  • windows下Anaconda的安装与配置正解(Anaconda入门教程) <font color=red>原创</font>

    2023-05-23 11:10:50
  • Python远程控制Windows服务器的方法详解

    2023-07-13 13:14:03
  • python 实现倒计时功能(gui界面)

    2021-03-05 14:19:55
  • python实现Oracle查询分组的方法示例

    2021-03-30 10:59:54
  • Python使用tkinter制作在线翻译软件

    2021-04-19 10:36:06
  • 使用PyQt的QLabel组件实现选定目标框功能的方法示例

    2022-01-08 22:43:21
  • python的random模块及加权随机算法的python实现方法

    2023-09-04 13:32:57
  • 初识Golang Mutex互斥锁的使用

    2024-05-09 09:39:57
  • python3发送request请求及查看返回结果实例

    2023-09-18 11:34:10
  • 常用的数据库备份类型有哪些?

    2009-11-01 13:02:00
  • vue-Split实现面板分割

    2024-04-27 15:49:43
  • 使用Python和xlwt向Excel文件中写入中文的实例

    2023-08-27 19:18:08
  • python使用pyaudio录音和格式转化方式

    2023-11-07 19:30:03
  • asp之家 网络编程 m.aspxhome.com