-
Context is the value of
thisin each part of your code. -
Function context depends on how the function is called, this is called the invocation pattern.
-
Global context:
thisis bound to global object when used globally. -
Function Context:
-
As Function: function is just called ,
thisis bound to globalwindowobject. -
As Method: function is called under object,
thisis bound to that object.
-
Special case functions like ,
bind,call,applycan change this context to any passed object. -
Construction of new Object: when we use
newkeyword to construct a new object, this is bound to the new created object.
this is bound to global object.
console.log(this); //global window object if in browserthis inside a function is bound to the object that called the function
function globalFunction(){
console.log(this); //this = window --> because called by global window object
}
globalFunction(); //this line is equivalent to window.globalFunction();
var myObject = {
myFunc : function(){
console.log(this);
}
}
myObject.myFunc(); //this = MyObject --> because the function is called on myObject
var temp = myObject.myFunc;
temp(); //this = window --> because the function is called and executed from global window objectThese three methods can be used to change this context value inside any function to any required object.
bind : creates a new function that, when called, has its this keyword set to the provided value, it also accept optional arguments that will be prepended to the argument list of the new returned function.
var obj= {
first: "Amr",
last: "Labib"
}
function printName(){
console.log(this.first + " " + this.last , arguments[0] , arguments[1]);
}
printName(); // undefined undefined undefined undefined --> because `this` is bound to global window object, also we don't have any argument passed to the function
var boundPrintName = printName.bind(obj); // will return a new function with `this` bound to obj
boundPrintName(); // Amr Labib undefined undefined
var boundPrintNameWithArgument = printName.bind(obj , "argument 1"); //will return new function with `this` bound to obj and first argument set to "argument 1"
boundPrintNameWithArgument("argument 2"); //Amr Labib argument 1 argument 2call: calls a function with a given this value and arguments provided individually.
apply: calls a function with a given this value, and arguments provided as an array
Notice the difference between call and apply is just how they accept arguments after passed this object.
var obj= {
first: "Amr",
last: "Labib"
}
function printName(){
console.log(this.first + " " + this.last , arguments[0] , arguments[1] );
}
printName(); // undefined undefined undefined undefined --> because this is bound to global window object, also we don't have any argument passed to the function
printName.call(obj); // Amr Labib undefined undefined
printName.call(obj , "argument 1" , "argument 2"); //Amr Labib argument 1 argument 2
printName.apply(obj); // Amr Labib undefined undefined
printName.apply(obj , ["argument 1" , "argument 2"]); //Amr Labib argument 1 argument 2Construction of new object
var Name = function(){
this.first = "Amr";
this.last = "Labib";
}
var obj1 = new Name(); //this == Name Object
console.log(obj1.first);Hint: By Convention we capitalize the first letter in function when we use it as Object constructor.
Use call with self invoked function to set this context inside each loop iteration
var numbers = [{
number : {
value : 1
}
},{
number : {
value : 2
}
},{
number : {
value : 3
}
}];
for (var i = 0; i < numbers.length; i++) {
(function () {
console.log(this);
}).call(numbers[i].number);
}
// {value: 1}
// {value: 2}
// {value: 3}