• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Declaring Constructors in TypeScript Angular 2

September 23, 2017 by ProjectsGeek Leave a Comment

How to Declare Constructors in TypeScript Angular 2

 

Constructors in TypeScript

In this post we will see how to create a constructors in TypeScript angular 2. S0 here we have same Point class which we used in tutorial for declaring class in Typescript.  Only code we added is for the constructor as given below. 

We can declare a constructor in TypeScript using ‘constructor’ keyword and we can have only one constructor. If you are using java you have feature of having multiple constructors inside a class but in typescript you can have only one constructor.  

class Point {
    
        x: number;
        y: number;

        constructor(x:number,y:number){
            this.x=x;
            this.y=y;
        }

        doSomething() {
            console.log('Point X: '+this.x);
            console.log('Point Y: '+this.y);
        }
    
    }

Lets run this above code to see the output.

PS C:\Users\AnilKumar\ts-hello> tsc code-main.ts
PS C:\Users\AnilKumar\ts-hello> node code-main.js
Point X: 1
Point Y: 2
PS C:\Users\AnilKumar\ts-hello>

How to make Constructor Optional in TypeScript?

We can use ‘?‘ to make a constructor parameter optional.

NOTE: All parameters right to optional parameter also need to be changed to optional else it will show you compilation error message.

class Point {
    
        x: number;
        y: number;

        constructor(x?:number,y?:number){
            this.x=x;
            this.y=y;
        }

        doSomething() {
            console.log('Point X: '+this.x);
            console.log('Point Y: '+this.y);
        }
    
    }

let point: Point;
point = new Point(1);

point.doSomething();

In above code we are passing only one parameter in constructor which is acting like a overloaded constructor feature only. Now lets run this code.

PS C:\Users\AnilKumar\ts-hello> tsc code-main.ts
PS C:\Users\AnilKumar\ts-hello> node code-main.js
Point X: 1
Point Y: undefined
PS C:\Users\AnilKumar\ts-hello>

 

Other Projects to Try:

  1. Declaring Interface in TypeScript Angular 2
  2. Declaring Classes in TypeScript Angular 2
  3. Difference between VAR and LET Angular 2
  4. Public and Private Keywords
  5. Chat Server project in Java

Filed Under: Angular 2 Tagged With: Angular 2

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy