TypeScript Class, Constructor and Object Creation


TypeScript follows ECMAScript 2015, also known as ECMAScript 6 standard that allows JavaScript developers to build their applications using object-oriented class-based approach. With object oriented you can create classes where class can inherit functionality and objects are built from these classes.

In TypeScript developers are free to use these object oriented technique to write JavaScript code and can compile them into JavaScript that support all the major browsers and platforms. Writing class in TypeScript is pretty simple if you are C# or Java developers.

class Customer{
    fname: string;
    lname: string;
    address: string;
    phone: string;
    constructor(fname: string, lname: string, address: string, phone: string) {
        this.fname = fname;
        this.lname = lname;
	 this.address = address;
	 this.phone = phone;
    }
    printCustomer() {
        return "Hello, " + this.fname + "  " + this.lname;
    }
}
var cust = new Customer("Jimi", "Scott", "12-13/A12", "984563332");
alert(cust.printCustomer());

In the above example, a new class Customer has created. This class created with four properties, one constructor and method called printCustomer. In the class, notice that member are using with this keyword which denoted that it's member access. In the code example class instance are created using new keyword by passing parameters value. This will run class constructor and assign passed value to the class members. Next, class object is used to call member function of the class called printCustomer. This member function will display customer first name and last name prefix with "Hello".

Live Demo


See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.