Objects 

Only having primitive data types and built-in complex data types is not enough when writing expressive software that attempts to model the real world or fictitious worlds (in gaming). The solution is to have programming languages with a construct for creating custom objects. Fortunately, JavaScript, and thus TypeScript, is a programming language that allows for the creation of custom objects. An object in JavaScript is a collection of mapped keys and values, where a key can be either a string or a symbol. This is similar to the case for many other programming languages, such as for Python's dictionary, and Ruby's hash. Not to get too technical just for the sake of being technical (which is a pet peeve of mine, and maybe is one for you as well), but JavaScript is not a classical object-oriented language. Rather, JavaScript uses prototypal inheritance to create other objects instead of creating an instance of an object from a class definition. In other words, JavaScript doesn't have the notion of classes. JavaScript has prototypes. Objects in JavaScript inherit directly from other objects. In fact, when you create an empty object in JavaScript using curly braces, it's really syntactic sugar for using the create method of the built-in object. There are several ways available to you for creating an empty object in JavaScript. We won't cover them all here, but we'll cover two of the ways that have been available in JavaScript for many years, and are made available to us in ES6:

  • Using the Object constructor: var myObject = new Object();
  • Using the curly brace syntax: var myObject = {};
  • Using ES6 class syntax (we'll get to the syntax in the following Classes section)

The first two methods create an empty object. If you want to create an empty object in JavaScript with minimal fuss, the second approach is obviously the easiest. However, the third approach, ES6 class syntax, is what we'll be using in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.90