Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content