Changing the context of a function in JavaScript -


This is taken from the advanced JavaScript # 25 of John Recife, which is called changing the context of the function.

1) In line fn () == what is it mentioned? Does it mention it inside the ceremony where it says that it comes back?

2) Although I understand the purpose of the last line (to attach a specific object to the function), I do not know how it codifies. Is the word "call" a pre-defined javascript function? In plain language, please explain "fn.call (object)", and clearly tell me if P>

3). After the function is assigned to the object, do you have object.fn (); ?

  var object = {} will call that function? Function (fn) () this return; } Insert (fn () == This, "Reference is a global object."); (Object) == object, "Context has been changed to a specific object."  

is a function defined for a function object. The first parameter for call is the object which is the reference to this function is being called.

When fn () is called without any special reference, refers to this global context, or window Browser environment This rule is applied in the global criteria of this , hence fn () == this) , this refers to the global object However, when it is said in the context of another object, as in fn.call (object) , then it inside fn Refers to Object .

does not modify or assign anything to fn.call (object) to object . The only affected code is this inside the value of fn for the duration of that call. Even after this call, you will continue to fn () , and object.fn () . Not in the form of

The example only indicates that the this value is dynamic inside the function.

Comments