<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[success] # ES13 -- at 1. **at()** 方法接收一個整數值并返回該索引的項目,允許正數和負數。負整數從數組中的最后一個項目開始倒數。 2. **數組** 和**字符串**都可以使用自身 **at** 方法 >[danger] ##### 案例 * 原先獲取元素 ~~~ const arr = ['a', 'b', 'c', 'd']; // 倒數第一個元素 console.log(arr[arr.length - 1]); // d // 倒數第二個元素 console.log(arr[arr.length - 2]); // c // 獲取第一個 console.log(arr[1]); // c ~~~ * 通過at 獲取 ~~~ const arr = ['a', 'b', 'c', 'd']; arr.at(-1) // d arr.at(0) // a ~~~ >[success] # ES13 -- Object.hasOwn 1. Object中新增了一個靜態方法(類方法)`hasOwn(obj, propKey)`該方法用于判斷一個對象中是否有某個自己的屬 2. 在之前有個類似方法就是 `hasOwnProperty`這個方法是個**實例方法** 屬于**Object.property** 原型鏈上,和ES13 新增的靜態方法相比, 2.1. **防止對象內部有重寫hasOwnProperty** ~~~ class Car { color = 'green'; age = 2; // 你看這個方法就沒有告訴我們這個類的對象是不是有某個屬性 hasOwnProperty() { return false; } } const car = new Car(); console.log(car.hasOwnProperty('age')); // false console.log(car.hasOwnProperty('name')); // false ~~~ 2.2. **對于隱式原型指向null的對象, hasOwnProperty無法進行判斷** * 因為沒有沖Object 對象繼承過來原型方法,因此調用會報錯 ~~~ const obj = Object.create(null); obj.color = 'green'; obj.age = 2; // TypeError: obj.hasOwnProperty is not a function console.log(obj.hasOwnProperty('color')); ~~~ ![](https://img.kancloud.cn/54/9d/549d40e21e8b3408491ab9a4edbaa50f_365x59.png) 想不報錯就需要利用call 去改變this 指向調用 ~~~ Object.prototype.hasOwnProperty.call(obj, 'color') ~~~ 相比之下ES13 增加的靜態方法更容易 ~~~ Object.hasOwn(obj, 'color') ~~~ >[danger] ##### 案例 * 使用原來 **hasOwnProperty** 方法 ~~~ class A { name = 'w' getName() { return this.name } } class B extends A { age = 12 } const b = new B() console.log(b.hasOwnProperty('name')) // true console.log(b.hasOwnProperty('age')) // true console.log(b.hasOwnProperty('getName')) // false console.log(Object.prototype.hasOwnProperty(b, 'getName')) // false ~~~ ![](https://img.kancloud.cn/47/e5/47e599223de9a1185b7ff6e9587d992d_358x182.png) * 使用靜態方法 **Object.hasOwn** ~~~ class A { name = 'w' getName() { return this.name } } class B extends A { age = 12 } const b = new B() console.log(Object.hasOwn(b, 'name')) // true console.log(Object.hasOwn(b, 'age')) // true console.log(Object.hasOwn(b, 'getName')) // false ~~~ >[success] # ES13 - class 1. **Class Public Instance Fields** 公共實例字段,類的實例字段是在對應的構造函數運行之前添加的,所以你可以在構造函數中訪問字段的值,相比以前只能在構造函數中定義屬性,現在可以利用共有實例字段直接定義屬性 ~~~ class ClassWithInstanceField { instanceField = 'instance field'; constructor() { console.log(this.instanceField);// 類的實例字段是在對應的構造函數運行之前添加的 this.instanceField = 'new value'; } } const instance = new ClassWithInstanceField(); // 輸出 "instance field" console.log(instance.instanceField); // "new value" ~~~ 2. **Private static class fields and methods 靜態私有字段和方法**,**Static class fields and methods 靜態公共字段和方法**, js 也支持私有屬性 方法等,用'#' 來修飾 ~~~ class Counter { #num = 0 // 私有屬性 static #baseNum = 100 // 靜態私有屬性 // 靜態私有方法 static getBaseNum() { return Counter.#baseNum } // 私有訪問器方法 get #getNum() { return this.#num } // 私有訪問器方法 set #initNum(num) { this.#num = num } // 私有方法 #getNumFun() { return this.#num } } ~~~ ![](https://img.kancloud.cn/74/0d/740d04aee2110b8f9d49d741deb910d3_575x194.png) 3. **Class Static Block 類靜態初始化塊**,只會在類被創造的時候執行一次 ~~~ class Car { static colors = [] num = 1 static { // 這時候訪問不到實例方法本質是undefined,只能訪問靜態屬性方法 console.log(this.num, 1111111) } static { this.colors.push('green') } } // 打印結果 undefined 1111111 ~~~ * 修改訪問靜態屬性 ~~~ class Counter { static #baseNum = 100; static getDoubleBaseNum() { return this.#baseNum * 2; } static { this.#baseNum = 200; } } console.log(Counter.getDoubleBaseNum()); // 400 ~~~ * 利用靜態塊訪問私有屬性 ~~~ let getDPrivateField; class D { #privateField; constructor(v) { this.#privateField = v; } static { getDPrivateField = (d) => d.#privateField; } } getDPrivateField(new D('private')); // > private ~~~ >[success] # ES13 -- 使用in來判斷某個對象是否擁有某個私有屬性 1. `#a in obj`(注意,與公有方法不同,這個判斷左側不是字符串,公有方法的判斷是`'a' in obj`) ~~~ class Car { #color; hasColor() { return #color in this; } } const car = new Car(); console.log(car.hasColor()); // true #color in car // 報錯 只能在類中使用,類外in 是需要字符串 '#color' in car // false 但是 字符串形式是檢測不到私有屬性 ~~~ >[success] # ES13 - top await 1. `wait`的時候有個很頭疼的地方就是一定要在一個`async`的函數里面使用而不能在全局作用域里面使用 ~~~ function setTimeoutAsync(timeout) { return new Promise((resolve) => { setTimeout(() => { resolve(); }, timeout); }); } // SyntaxError: await is only valid in async functions await setTimeoutAsync(3000); ~~~ * 新版后 ~~~ function setTimeoutAsync(timeout) { return new Promise((resolve) => { setTimeout(() => { resolve(); }, timeout); }) } // 慢慢地等時間流逝吧 await setTimeoutAsync(3000); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看