CodesJava

Easy learning with example program codes

typescript object tutorial

TypeScript Object:

A real world entity is known as an object. Like every real world object, software objects also have state and behavior. State of the object is represented by data members or fields and behavior is represented by methods.

In TypeScript an object represents an instance which contains set of key value pairs. The values can be scalar values, functions or array of other objects.

Syntax:

var objectName = { 
   key1: “value”, //scalar value 
   key2: function() {
      //functions 
   }, 
   Key3:[“content1”, “content2”] //collection  
};

Example:

class Employee { 
   //field 
   name:string; 
   empId:string;  
} 
//function
var display = function(obj: {name:string, empId:string}) { 
  console.log("Employee Name: "+obj.name);
  console.log("Employee EmpId: "+obj.empId);
} 
 
//create an object 
var obj:Employee = {name:"Jai", empId:"EMP024"};
 
//access the function
display(obj);


Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status