Javascript lost context when assigned to other variable

后端 未结 4 981
一向
一向 2020-11-30 13:28

Why in javascript if you reference objects method to some variable it loses that objects context. Can\'t find any link with explanation what happens under the hood. Except t

4条回答
  •  無奈伤痛
    2020-11-30 13:50

    From Douglas Crockford's book JavaScript: The Good Parts

    The this parameter is very important in object oriented programming, and its value is determined by the invocation pattern. There are four patterns of invocation in JavaScript: the method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the apply invocation pattern. The patterns differ in how the bonus parameter this is initialized

    The Method Invocation Pattern

    When a function is stored as a property of an object, we call it a method. When a method is invoked, this is bound to that object. If an invocation expression contains a refinement (that is, a . dot expression or[subscript] expression), it is invoked as a method

    In your example method invocation pattern is

    console.log(obj.method() === 1);
    

    and this in this case is bound to the object "Class" and it works as you expected.

    The Function Invocation Pattern

    When a function is not the property of an object, then it is invoked as a function:

    var sum = add(3, 4); // sum is 7

    When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language. Had the language been designed correctly, when the inner function is invoked, this would still be bound to the this variable of the outer function. A consequence of this error is that a method cannot employ an inner function to help it do its work because the inner function does not share the method’s access to the object as its this is bound to the wrong value

    In your case

    var refToMethod = obj.method; // why refToMethod 'this' is window
    console.log(refToMethod() !== 1) // why this is true?
    

    refToMethod is bound to the global object "window" in this case

    You can find more information about this at JavaScript “this” keyword

提交回复
热议问题