Define Typescript:-
TypeScript (TS) is an open source programming language developed by Microsoft. Typescript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces.
What is the different static and dynamic typing?
Static typing:-
It's all about the
variables. In statically typed program languages, if I create an integer, I
have to tell the compiler that it is an integer and will always be an integer.
Dynamic typing:-
In dynamic languages,
you don't specify the type and you can dynamically change the type. For
example, to declare a variable in Javascript, you would type var bar;. If I then set it to an
integer, it will work fine. bar = 5;.
I can then even later set the same variable to a string, bar = "Hello
World!"; and it will work fine! Hence the term dynamic.
Before we start
typescript lets show some ECMAScript 6 Features that help in typescript.
1.
Let keyword:-
Variables defined by the let keyword have a
following of advantages:
·
Given this way, the variable
is visible only within the block
·
Variable let cannot be
used until it has been declared
·
Can declare let variable
twice in same scope of declaration. If you declare then it’s give you error.
Example:-
for (let num = 5; num < 10; num++) {
console.log(num); // 5, 6, 7, 8, 9
}
console.log(num); // num is not defined
var number = 1;
if(number < 10) {
let v = 1;
v = v + 21;
v = v * 100;
v = v / 8;
console.log(v);
}
console.log(v); //v is not defined
2.
Classes:-
ES6
classes are a simple follow based OO pattern. Classes support inheritance,
super calls, instance and static methods and constructors to declare a
class, one should use keywords class and constructor. Now you can as well
inherit new classes using the "class Child extends Parent"
syntax.
Example:-
class Product {
constructor(price,
category, description) {
this.price
= price;
this.category =
category;
this.description
= description;
this.isPublished
= false;
}
publish()
{
this.isPublished
= true;
}
unpublish()
{
this.isPublished
= false;
}
static
getDate() {
return
new Date();
}
}
class DownlowdableProduct extends Product{
// your code goes here
}
In above example you can see that
·
the instace of object
can be created using new keyword
·
Static methods can also be declared in classes
3. Default
Value:-
To set default values for parameters.
Example:-
class Product {
constructor(price, category,
description, isPublished = false) {
this.price = price;
this.category = category;
this.description =
description;
this.isPublished =
isPublished;
}
publish()
{
this.isPublished = true;
}
unpublish()
{
this.isPublished = false;
}
}
In above example you can see that:-
·
set isPublished property contain default
value.
4. const
keyword:-
To declare a constant, you can use const keyword,
thus creating a variable that cannot be changed.
Example:-
const person = {
screenName:
"Guest"
};
person.screenName = "John"; // available
person = 5; // error
5. Template
String:-
They begin and end with quotation mark (`).In case
you use single and double quotation marks, it is allowed to use new lines and
substitute expressions using braces ${variable}.It use for avoiding
injection attacks or constructing higher level data structures from string
contents.
Example:-
class Product {
constructor(price,
description, isPublished = false) {
this.price
= price;
this.description
= description;
this.isPublished
= isPublished;
}
publish()
{
this.isPublished
= true;
}
unpublish()
{
this.isPublished = false;
}
}
class DownlowdableProduct extends Product {
constructor(price,
link, title, description, isPublished = false) {
super(price,
description, isPublished);
this.link
= link;
this.title
= title;
}
toString()
{
return
`<li>
${this.title} -
${this.link} <span>${this.price}</span>
</li>`;
}
}
know more features, so stay tune... :)
Comments
Post a Comment