What does 'this' mean?
//=======================================================
// Remember the acronym GONE (Global,Object,New,Explicit)
// It describes the different execution contexts within
// which "this" is evaluated in your code.
//=======================================================//=======================================================
//Global Execution Context
//=======================================================
this.console; // (1)
//=======================================================
// (1) Outside of a function, "this" means the global object.
// In a browser the global object is the window object.
// So the code above is the same as window.console;
//=======================================================//=======================================================
// Object Execution Context
//=======================================================
var square = {
width: 5,
height: 5,
getArea: function () {
return this.width * this.height; // (1)
}
}
console.log("Area of square is " + square.getArea());
//=======================================================
// (1) When a function is called on an object, the value
// of "this" inside that function, is the object that
// the function is called on i.e. square.
//=======================================================PreviousHow can I build an object with computed property names?NextHow does inheritance work between objects?
Last updated
Was this helpful?