December 2017
Beginner
372 pages
10h 32m
English
The this keyword in JavaScript has always been a cause of confusion for developers. In traditional JavaScript, the lexical scope of the this keyword is based on the time when a function is executed.
Take a look at the following example to understand how the significance of this changes:
function Book(title) { this.title = title; this.printTitle = function () { this.title = this.title + " by Sachin Ohri"; console.log(this.title); }}var typeScript = new Book("TypeScript By Example");setTimeout(typeScript.printTitle, 1000);setTimeout(function() { typeScript.printTitle(); },2000)
When the preceding function is executed, we get the following output:
undefined by Sachin Ohri TypeScript By Example by Sachin Ohri ...
Read now
Unlock full access