Constructor in TypeScript
- constructor(ParameterList(Optional)) {
- ................
The following example tells you, how to use a constructor in TypeScript. Use the following procedure to create a program using a constructor.
Step 1
Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window will be opened. Specify the name of your application, like "ExampleOfConstructor". Your application will look like:

ExampleOfConstructor.ts
- class MyExample {
- public Fname: string;
- public Lname: string;
- constructor(Fname: string, Lname: string) {
- this.Fname = Fname;
- this.Lname = Lname;
- alert("Company name=" + this.Fname + " " + this.Lname);
- }
- }
- window.onload = () => {
- var data = new MyExample("Mcn", "Solution");
- }
Note In the above-declared program, I have the MyExample class, with a constructor which takes two string value as arguments. When I create an instance of the class (MyExample), the constructor is automatically executed.
default.html
- < !DOCTYPEhtml >
- <
- htmllang = "en"
- xmlns = "http://www.w3.org/1999/xhtml" >
- <
- head >
- <
- metacharset = "utf-8" / >
- <
- title > TypeScript HTML App < /title>
- <
- linkrel = "stylesheet"
- href = "app.css"
- type = "text/css" / >
- <
- scriptsrc = "app.js" > < /script>
- <
- /head>
- <
- body >
- <
- h1 > Example of Constructor < /h1>
- <
- divid = "content" / >
- <
- /body> <
- /html>
app.js
- var MyExample = (function() {
- function MyExample(Fname, Lname) {
- this.Fname = Fname;
- this.Lname = Lname;
- alert("Company name=" + this.Fname + " " + this.Lname);
- }
- return MyExample;
- })();
- window.onload = function() {
- var data = new MyExample("Mcn", "Solution");
- };
Output:


Gowtham RajamanickamPosted Apr 12, 2016, 2:17 AM
please write more article
Gowtham RajamanickamPosted Apr 12, 2016, 2:17 AM
Good One, Thanks for sharing