javascript - What is the purpose of using get before the method name in angular2? -


i new angular2 , when reviewing someone's code, 1 specific line got me confused

get formdata() { return <formarray>this.lienholder.get('policydetails'); } 

why above line different this

formdata() { return <formarray>this.lienholder.get('policydetails'); } 

i searched in google , found no actual results, can me understand this.

update

what difference between

var obj = { log: 0, latest() { return this.log++; } };  

and

var obj = { log: 0, latest() { return this.log++; } };  

both giving me updated value time call them obj.latest & obj.latest() -- returns updated result time why use 1 on another?

get formdata() 

is called getter accessor. allows property dynamically angular digest cycle. should return value.


https://www.typescriptlang.org/docs/handbook/classes.html

typescript supports getters/setters way of intercepting accesses member of object. gives way of having finer-grained control on how member accessed on each object.

https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions/get

sometimes desirable allow access property returns dynamically computed value, or may want reflect status of internal variable without requiring use of explicit method calls. in javascript, can accomplished use of getter.


in opposite getformdate() function, can take arguments , not return values.

one of cases, use getter when property should service:

<p>{{dictionary.label1}}</p> 

and service this:

get dictionary(){   return this.myservice.getdictionary(); } 

this way when service changed data dynamically can receive value binding/model.

if have defined following:

dictionary: [];  ngoninit(){   this.dictionary=this.myservice.getdictionary(); } 

then 'stuck' old data while service received new set of data. of course, can set change listener , trigger update, it's more code!

think of getters dynamic class properties.


update: examples in updated post, it's true, give same result, , it's thing! can use both, don't work similarly, can have more options in cases. it's not 1 or other, or 1 best. in of cases can use both, it's , need use them. of times, it's method used, has more comprehensive use: use methods or without parameters, trigger actions on objects. in cases, again, not have same flexibility getter. if hesitant 1 use, use method first , when see limits, think if getter you, know what's purpose, - property, dynamic!


an other example:

isshown:boolean; //is 'static', return same value unless change in kind of method  isshown(){    return this.somecondition && this.somemethodresult() || this.anothercondition } 

if somecondition , anothercondition change , result somemethodresult had come changed don't have request isshown value, it's done dynamically.

opposite of that, can have

setshown(){ //the method   this.isshow=!this.isshown; } 

here setshown needs called isshown updated.

also, getter can replace method job return class property value.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -