The Complete Rules to `this`
Master 'this' by learning exactly how its rules work in JavaScript. Eliminate the cloud of uncertainty surrounding this keyword. Learn exactly how 'this' is determined, how to predict what it will be before you run your code, and how to leverage its uses.
this
is set in several different ways inside functions. This lesson will make you a master of this
.
Rules
1 - If the new
keyword is used when calling the function, this
inside the function is a brand new object created by the JavaScript engine.
Press + to interact
Javascript (babel-node)
function ConstructorExample() {console.log(this);this.value = 10;console.log(this);}new ConstructorExample();// -> ConstructorExample {}// -> ConstructorExample { value: 10 }