Property can be used to set or get private field of a class
Property declaration requires 2 accessors set and get
Syntax:
public set <propertyname>(para)
{
set private field and perform calculation (or) validation
}
public get <propertyname>():<type>
{
return private field
}
Property is a combination of 2 blocks 1.set block--writting to a property will execute set block 2.get block--reading a property will execute get block Example
Emp class | sal property[set{da=sal*0.3} and get{..} blocks] _sal private field _da private field e=new emp(); e.sal=10000;-->set block execution[da=3000] e.sal=e.sal+5000; | reading[get block execution] | 10000+5000 | 15000 | set block exec[da=4500] Property looks like normal public field, but internal execution will be like methods.
Property is recommended when you want to perform calculation validation based on specific private field.
Example
Note:
Accessors are only available when targeting ECMAScript 5 and higherOutputempno:1 sal:10000 da:3000 empno:1 sal:15000 da:4500
Note:
*target ECMAScript version can be given to tsc using "target[-t]" option syntax: tsc <tsfilename> -t ecmascript version[es5|es6|..]
Sample way of calling ECMAScript
*goto terminal >tsc propertydemo.ts -t es5 >node propertydemo
No comments:
Post a Comment
Thank you for visiting my blog