The this operator in a function context

The value of this inside a function depends on how the function is invoked. If we simply invoke a function in non-strict mode, the value of this within the function will point to the global object as follows:

function f1() { 
  return this; 
} 
f1() === window; // true 
All examples in this sub section (that is, the this operator in a function context) are JavaScript examples, not TypeScript examples.

However, if we invoke a function in strict mode, the value of this within the function's body will point to undefined as follows:

console.log(this); // global (window) 
 
function f2() { 
  "use strict"; 
  return this; // undefined 
} 
 
console.log(f2()); // undefined 
console.log(this); // window 
ECMAScript 5's strict ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.