![]() |
TypeScript is an open-source programming language developed and maintained by Microsoft. It serves as a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. It’s mainly designed for large-scale projects. TypeScript comes with extra added features like Static Type Checking, Modularity, Class-Based Objects, Modularity, ES6 Features, and a Syntax similar to High-Level Languages like Java. Creating Objects in Typescript:Now, let us see multiple ways in which objects can be created using Typescript. Fundamentally, JavaScript runs with Template-based code snippets, we can create objects directly without creating classes, with the help of Object literal and constructor methods. 1. Object Literals:Object literals are sets of name-value pairs stored in comma-separated lists. Syntax: let Name_of_object = { Example to Create Object using Object Literals: In this example, we will create an object in typescript.
Output: 2. Constructor Method:Constructor methods are used for initializing objects created within a class. Only one constructor method is allowed per class. Syntax: function Name_Of_Constructor( property1, property2, ...) {} Inside Constructor: Inside this Constructor method, we can initiate parameter values to properties of objects using the “this” keyword. function Name_Of_Constructor( property1, property2, ...) { or We can declare both properties of the object and parameter with the same name. function Name_Of_Constructor( property1, property2, ...) { Explanation: “this” Keyword references object property with the required parameters, to simply say “this” keyword represents the object to which we are initiating parameters with the help of the constructor method. Example to Create Object using Constructor Method: In this example, we will use the constructor method.
Output: 3. Passing Objects as Function Parameters:Objects can be passed as arguments to functions in TypeScript, specifying required object properties within the function definition. Syntax: let Name_Of_Object { Example: In this example, we will pass an object as a parameter to functions.
Output: 4) Using Object.create() method:The Object.create() method creates a new object with the specified prototype object and properties. It allows you to create objects that inherit properties from another object (referred to as the prototype), without calling a constructor function. Syntax: let newObj = Object.create(proto[, propertiesObject]);
Example: In this example, we will create object using Object.create() method.
Output: 5) Using Classes:Classes in TypeScript provide a syntax for creating objects with predefined properties and methods. They offer a more structured and object-oriented approach to defining object blueprints, encapsulating data, and behavior. Syntax: class ClassName {
Example: In this example, we will create object using Class.
Ouput: Key Features of TypeScript
Conclusion:These methods illustrate various approaches to object creation in TypeScript, offering flexibility and efficiency in managing data structures within the codebase. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 8 |