<thead id="rrjt3"></thead>
      <progress id="rrjt3"><dfn id="rrjt3"></dfn></progress>

      <em id="rrjt3"></em>

        <address id="rrjt3"><ins id="rrjt3"><dfn id="rrjt3"></dfn></ins></address>
          <i id="rrjt3"></i>

        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


        黄网站免费 <