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 call, apply, and bind methods

All functions inherit the call, apply, and bind methods from Function.prototype. We can use these methods to set the value of this.

The call and apply methods are almost identical; both methods allow us to invoke a function and set the value of the this operator within the function. The main difference between call and apply is that, while apply lets us invoke the function with arguments as an array, call requires the function parameters to be listed explicitly.

A useful mnemonic is A (apply) for an array and C (call) for a comma.

Let's look at an example. We will start by declaring a class named Person. This class has two properties (name and surname) and one method (greet). The greet method uses the this ...

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