wtorek, 7 kwietnia 2015

Javascript getter method...

Every day brings something new :-) Recently I was reading Eloquent JavaScript: A Modern Introduction to Programming by Marijn Haverbeke, and I was working with below code from book.

At first everything was very clear for me, ok it is just simple small JavaScript object.... but when I looked once again I saw this strange syntax near content function. It turned out that this is special syntax for getter method.

With getter method we can have property that returns a dynamically computed value or we can reflect the status of internal variable without the use of explicit method calls.

When using get syntax you need to be aware of:

  • it can have an identifier which is either a number or a string
  • it must have zero parameters
  • it must not appear in an object literal with another get or with a data entry for the same property.

A getter can be removed using delete operator.

And there is of course setter method :-). For more info please look on MDN site.