js函數( 普通函數、箭頭函數 ) 內部this的指向

- 普通函數

  | 具名普通函數、匿名普通函數,在不作為對象的屬性值的情況下,其內部的 this 總是指向程式碼運行環境下的全局對象 ( 例如,瀏覽器中的 window )。

    示例:

    (function() {
        console.log(this); // window
        (function() {
            console.log(this); // window
            (function() {
                console.log(this); // window
            })()
        })()
    })()

  

  | 普通函數,均可以通過其 bind、call、apply 方法 來改變其內部 this 的指向。

    示例:

    (function() {
        const func = (function() { console.log(this) }).bind('hello')
        const obj = {
            func,
            func1: (function() { console.log(this) }).bind('hello'),
            func2: (function F() { console.log(this) }).bind('hello')
        }
        func() // String {'hello'}
        obj.func() // String {'hello'}
        obj.func1() // String {'hello'}
        obj.func2() // String {'hello'}
    })()

  

  | 當普通函數( 具名的、匿名的、外部定義的方法 ),作為對象的屬性值被引用的時候,其內部的 this 指向該屬性所直接歸屬的對象 。

    示例:

    (function() {
        const func = function() { console.log(this) }
        const obj = {
            func,
            func1: function F() { console.log(this) },
            func2() { console.log(this) },
            param: {
                func,
                func1: function F() { console.log(this) },
                func2() { console.log(this) }
            }
        }
        func() // window
        obj.func() // obj
        obj.func1() // obj
        obj.func2() // obj
        obj.param.func() // obj.param
        obj.param.func1() // obj.param
        obj.param.func2() // obj.param
    })()

  

 


- 箭頭函數

  | 箭頭函數,不管是作為獨立的方法 或是 作為對象的屬性值,其內部的 this 均指向 該箭頭函數被定義時所在的上下文中對應的 this。

    示例:

    (function() {
        /** 外層作用域 */
        const arrowfunc = () => console.log(this)
       
        console.log('-- 外層作用域 --');
        console.log(this); // String {'hello'}
        arrowfunc(); // String {'hello'}
       
        (function() {
            /** 內層作用域 */
            const arrowfunc1 = () => console.log(this)
           
            console.log('-- 內層作用域 --');
            console.log(this); // String {'world'}
            arrowfunc() // String {'hello'}
            arrowfunc1() // String {'world'}

            /** 函數作為對象屬性值 */
            const obj = {
                arrowfunc,
                arrowfunc1,
                param: {
                    arrowfunc,
                    arrowfunc1,
                    arrowfunc2: () => console.log(this)
                }
            }
           
            console.log('-- 函數作為對象屬性值 --');
            obj.arrowfunc() // String {'hello'}
            obj.arrowfunc1() // String {'world'}
            obj.param.arrowfunc() // String {'hello'}
            obj.param.arrowfunc1() // String {'world'}
            obj.param.arrowfunc2() // String {'world'}
        }).bind('world')()
    }).bind('hello')()

  

  | 箭頭函數 也有 bind、call、apply 方法,與普通函數一樣可以通過這三個方法預設箭頭函數的入參值。

    試圖通過這三個方法改變箭頭函數內部 this 的指向,雖不會報錯但卻是無效的。

    示例:

    (function() {
        console.log(this); // String {'hello'}
        (() => {
            console.log(this); // String {'hello'}
            (() => {
                console.log(this) // String {'hello'}
            }).bind('bbb')()
        }).bind('aaa')();
       
        ((a, b, c) => {
            console.log(this) // String {'hello'}
            console.log(a) // a
            console.log(b) // b
            console.log(c) // c
        }).bind(null, 1, 2)(3)
    }).bind('hello')()

  

  | 附:

      * 箭頭函數不能作為構造函數使用,強制使用 new 運算符作用在箭頭函數上,將會報如下錯誤

         new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor

  

      * 箭頭函數內部沒有定義 arguments 變數,箭頭函數所在的作用域也不存在 arguments 的情況下,應用該變數會報錯。

        (function() {
            ((a) => {
                console.log(a) // 1
                console.log(arguments) // Arguments ['hello']
            })(1)
        })('hello');

        (() => {
            console.log(arguments) // Uncaught ReferenceError: arguments is not defined
        })();

 

      * 普通函數都有原型屬性 prototype,箭頭函數沒有這個屬性。

        (function() {}).prototype // {constructor: ƒ}
        (() => {}).prototype // undefined

  

 
 
Tags: