☰ See All Chapters |
TypeScript Object/object
An Object is an instance which contains set of key value pairs. The values can be scalar values, functions, array of other Objects and Tuples. TypeScript has not one but two types related to objects: Object and object. (One starts from uppercase and other with lower case)
Object: A variable of the Object type is similar to a JavaScript Object (set of key value pairs).
object: object type is the instance of the class and interfaces, which we will be see later.
Object Syntax:
Each value of an Object should be associated with key identifier. When initializing an object, key-value pairs are surrounded by curly braces instead of square brackets. keys of an object is not necessary to surrounded in quotes, though they can be. If an object's value is surrounded by quotes, it will be interpreted as a string.
var object_name = { key1: value1, key2: value2, key3: function() { //functions }, key4:[“content1”, “content2”] //collection }; |
Object values are accessed by using key not by index and hence the order of values in an object isn't important.
var name_object = { firstName: "Manu", lastName: "Manjunatha" }; |
Is same as
var name_object = { lastName: "Manjunatha", firstName: "Manu" }; |
How to access Object value
Object values are accessed by using key with dot notation.
var authorName = { lastName: "Manjunatha", firstName: "Manu" }; console.log(authorName.firstName); console.log(authorName.lastName); |
All Chapters