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

Instance properties versus class properties

Because JavaScript is a dynamic programming language, we can add properties and methods to an instance of an object at runtime, and they don't need to be part of the object (class) itself. Let's look at an example:

function Person(name, surname) { 
      // instance properties 
      this.name = name; 
      this.surname = surname; 
} 
const person = new Person("Remo", "Jansen"); 
person.age = 27; 

We have defined a constructor function for an object named person, which takes two variables (name and surname) as arguments. Then, we have created an instance of the Person object and added a new property named age to it. We can use a for...in statement to check the properties of person at runtime:

for(let property in person) ...
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