<noframes id="7xbfr"><pre id="7xbfr"><output id="7xbfr"></output></pre>

      <big id="7xbfr"><font id="7xbfr"><listing id="7xbfr"></listing></font></big>
        <p id="7xbfr"></p>
        <menuitem id="7xbfr"></menuitem>

          <video id="7xbfr"><mark id="7xbfr"><listing id="7xbfr"></listing></mark></video>
            <form id="7xbfr"></form>

          <delect id="7xbfr"><var id="7xbfr"></var></delect>

          JS中的CALL()方法的理解

          時間:2021-05-08 12:34:35 類型:JS/JQUERY
          字號:    
          fn.call(obj,arg1,arg2);
          這是call()方法的使用形式,apply()是差不多的。作用是用obj對象來替換fn中的this
          舉個栗子:
          function A(){
              this.color="blue";
          }
          function B(){ 
              this.color="yellow"; 
          } 
          var a=new A();
          B.call(a);  //用a對象來替換B()函數中的this;函數在調用call()或apply()的時候會執行函數
          alert(a.color);  //這里將輸出yellow
          // 因此,上面的代碼就可以改寫成:
          function A(){
              this.color="blue";
          }
          function B(){ 
              a.color="yellow";  //上面的B.call(a)的結果就變成了這樣
          } 
          var a=new A();
          B();  //調用了一次B(),從而修改了a.color的值
          alert(a.color);  //所以輸出yellow


          黄网站免费 <