<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                >[success] # 只能通過new創建實例 ~~~ 1.構造函數,不僅可以通過new 創建,也可以直接調用,但 如何限制構造函數只能通過new 創建實例es6 給了一個新的 方法new.target 2.js 函數中有兩個不同的內部方法:'[[Call]]' 和 '[[Construct]]' 當通過'new' 關鍵字調用函數的時候,執行的是'[[Construct]]', 負責創建實例對象,然后執行函數體,將'this'綁定在實例上,如果不 通過'new' 創建,調用'[[Call]]' 直接執行函數體代碼,具有 '[[Construct]]' 方法函數叫構造函數 ~~~ >[danger] ##### es5 判斷當前創建的是否是實例對象 ~~~ 1. 下面的方法就是利用'instanceof' 來判斷'this' 類型,首先根據上面 第二條的解釋如果通過'new'創建的,就會先執行'[[Construct]]',再創建 實例,然后創建函數體,將this綁定在實例上,因此此時的'this'就可以用 'instanceof' 判斷是不是通過'new'創建的 2.下面的第二個'Person.call(person)' 就是來說這種es5 的寫法不是最安全的 因為只是單純的判斷'this',因此可以用'call' 來繞過邏輯 3.下面的第三個就會直接報錯因為不是通過'new'創建的 ~~~ ~~~ function Person(){ if(this instanceof Person){ this.name = 'wang' }else{ throw new Error('必須通過new關鍵字調用person') } } const person = new Person() // 1 Person.call(person) // 2 Person() // 3 ~~~ >[danger] ##### es6 新方法new.target ~~~ 1.執行了'[[Call]]'方法 'new.target'就是 'undefined' 當調用 '[[Construct]]' new操作符的目標也是就創建的對象實例 ~~~ ~~~ function Person(){ if(typeof new.target !=='undefined'){ console.log(new.target) this.name = 'wang' }else{ throw new Error('必須通過new關鍵字調用person') } } const person = new Person() // 1 Person.call(person) // 報錯 // 打印結果: ? Person(){ if(typeof new.target !=='undefined'){ console.log(new.target) this.name = 'wang' }else{ throw new Error('必須通過new關鍵字調用person') } … ~~~ * 或者使用new.target判斷調用的構造函數 ~~~ function Person(){ if( new.target ===Person){ this.name = 'wang' }else{ throw new Error('必須通過new關鍵字調用person') } } const person = new Person() // 正常創建 Person.call(person) // 報錯 ~~~
                  <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>

                              哎呀哎呀视频在线观看