1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      ES6怎么定義類

      這篇文章主要介紹“ES6怎么定義類”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“ES6怎么定義類”文章能幫助大家解決問(wèn)題。

      邳州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

      在ES6中,class(類)作為對(duì)象的模板被引入,可以通過(guò)“class”關(guān)鍵字來(lái)定義類。class的本質(zhì)是function,它可以看作一個(gè)語(yǔ)法糖,讓對(duì)象原型的寫法更加清晰、更像面向?qū)ο缶幊痰恼Z(yǔ)法。

      本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。

      ES6 Class

      在ES6中,class(類)作為對(duì)象的模板被引入,可以通過(guò)“class”關(guān)鍵字來(lái)定義類。

      class 的本質(zhì)是 function。

      基本上,ES6的class可以看作只是一個(gè)語(yǔ)法糖,它的絕大部分功能,ES5都可以做到,新的class寫法只是讓對(duì)象原型的寫法更加清晰、更像面向?qū)ο缶幊痰恼Z(yǔ)法而已。

      基礎(chǔ)用法

      類定義

      類表達(dá)式可以為匿名或命名。

      // 匿名類
      let Example = class {
          constructor(a) {
              this.a = a;
          }
      }
      // 命名類
      let Example = class Example {
          constructor(a) {
              this.a = a;
          }
      }

      類聲明

      class Example {
          constructor(a) {
              this.a = a;
          }
      }

      注意要點(diǎn):不可重復(fù)聲明。

      class Example{}
      class Example{}
      // Uncaught SyntaxError: Identifier 'Example' has already been 
      // declared
       
      let Example1 = class{}
      class Example{}
      // Uncaught SyntaxError: Identifier 'Example' has already been 
      // declared

      注意要點(diǎn):

      類定義不會(huì)被提升,這意味著,必須在訪問(wèn)前對(duì)類進(jìn)行定義,否則就會(huì)報(bào)錯(cuò)。

      類中方法不需要 function 關(guān)鍵字。

      方法間不能加分號(hào)。

      new Example(); 
      class Example {}

      類的主體

      屬性

      prototype

      ES6 中,prototype 仍舊存在,雖然可以直接自類中定義方法,但是其實(shí)方法還是定義在 prototype 上的。 覆蓋方法 / 初始化時(shí)添加方法

      Example.prototype={
          //methods
      }

      添加方法

      Object.assign(Example.prototype,{
          //methods
      })

      靜態(tài)屬性

      靜態(tài)屬性:class 本身的屬性,即直接定義在類內(nèi)部的屬性( Class.propname ),不需要實(shí)例化。 ES6 中規(guī)定,Class 內(nèi)部只有靜態(tài)方法,沒(méi)有靜態(tài)屬性。

      class Example {
      // 新提案
          static a = 2;
      }
      // 目前可行寫法
      Example.b = 2;

      公共屬性

      class Example{}
      Example.prototype.a = 2;

      實(shí)例屬性

      實(shí)例屬性:定義在實(shí)例對(duì)象( this )上的屬性。

      class Example {
          a = 2;
          constructor () {
              console.log(this.a);
          }
      }

      name 屬性

      返回跟在 class 后的類名(存在時(shí))。

      let Example=class Exam {
          constructor(a) {
              this.a = a;
          }
      }
      console.log(Example.name); // Exam
       
      let Example=class {
          constructor(a) {
              this.a = a;
          }
      }
      console.log(Example.name); // Example

      方法

      constructor 方法

      constructor 方法是類的默認(rèn)方法,創(chuàng)建類的實(shí)例化對(duì)象時(shí)被調(diào)用。

      class Example{
          constructor(){
            console.log('我是constructor');
          }
      }
      new Example(); // 我是constructor

      返回對(duì)象

      class Test {
          constructor(){
              // 默認(rèn)返回實(shí)例對(duì)象 this
          }
      }
      console.log(new Test() instanceof Test); // true
       
      class Example {
          constructor(){
              // 指定返回對(duì)象
              return new Test();
          }
      }
      console.log(new Example() instanceof Example); // false

      靜態(tài)方法

      class Example{
          static sum(a, b) {
              console.log(a+b);
          }
      }
      Example.sum(1, 2); // 3

      原型方法

      class Example {
          sum(a, b) {
              console.log(a + b);
          }
      }
      let exam = new Example();
      exam.sum(1, 2); // 3

      實(shí)例方法

      class Example {
          constructor() {
              this.sum = (a, b) => {
                  console.log(a + b);
              }
          }
      }

      類的實(shí)例化

      new

      class 的實(shí)例化必須通過(guò) new 關(guān)鍵字。

      class Example {}
       
      let exam1 = Example(); 
      // Class constructor Example cannot be invoked without 'new'

      實(shí)例化對(duì)象

      共享原型對(duì)象

      class Example {
          constructor(a, b) {
              this.a = a;
              this.b = b;
              console.log('Example');
          }
          sum() {
              return this.a + this.b;
          }
      }
      let exam1 = new Example(2, 1);
      let exam2 = new Example(3, 1);
       
      // __proto__ 已廢棄,不建議使用
      // console.log(exam1.__proto__ == exam2.__proto__); 
       
      console.log(Object.getPrototypeOf(exam1) === Object.getPrototypeOf(exam2));// true
       
      Object.getPrototypeOf(exam1).sub = function() {
          return this.a - this.b;
      }
      console.log(exam1.sub()); // 1
      console.log(exam2.sub()); // 2

      關(guān)于“ES6怎么定義類”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。


      分享題目:ES6怎么定義類
      當(dāng)前鏈接:http://www.ef60e0e.cn/article/ghjeed.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        龙山县| 历史| 循化| 蛟河市| 巩义市| 湘乡市| 卓尼县| 惠水县| 突泉县| 蓬莱市| 体育| 巨鹿县| 双牌县| 霍城县| 新邵县| 丹江口市| 吉水县| 高陵县| 仪陇县| 三原县| 芜湖市| 内江市| 贵南县| 祥云县| 淅川县| 乌兰县| 贵港市| 资溪县| 德惠市| 新河县| 城口县| 黄石市| 巍山| 武功县| 洱源县| 五峰| 刚察县| 涞水县| 旬阳县| 金湖县| 芦山县|