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) ...

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.