6

Look at this script:

var human = 
{
   firstName: 'Saeed',
   lastName: 'Neamati',
   get fullName() {
       return this.firstName + ' ' + this.lastName;
   }
}

I don't know what get means in this context.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • possible duplicate of [Does Javascript have get/set keywords like C#?](http://stackoverflow.com/questions/5409372/does-javascript-have-get-set-keywords-like-c) – Tomasz Nurkiewicz Sep 13 '11 at 11:28

2 Answers2

7

It identifies an object property that's returned when the property is read.

See https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/get

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • This only works in ES5. It's saver to use `Object.defineProperty` with a proper shim – Raynos Sep 13 '11 at 11:33
  • It seems to be reasonably well supported in non-ie browsers; http://robertnyman.com/javascript/javascript-getters-setters.html#regular-getters-and-setters – Alex K. Sep 13 '11 at 11:35
  • [It works](http://robertnyman.com/javascript/javascript-getters-setters.html#regular-getters-and-setters) on Chrome, Safari, Firefox, and Opera. Not IE. OMG, Microsoft failed to adhere to a standard, making life worse for thousands of developers and millions of users! We better alert the media. This is going to be huge news! Microsoft's impeccable reputation will be in tatters! – Michael Lorton Sep 13 '11 at 11:39
  • @Malvolio It does work in all browser apart from IE<9. But the point is that this syntax can not degrade gracefully, ever. Almost all other ES5 features can degrade gracefully. – Raynos Sep 13 '11 at 11:41
  • 2
    @Raynos -- which is why I encourage people to take advantage of the IE [memory-cycle bug](http://javascript.crockford.com/memory/leak.html), so that users have more obvious reasons to upgrade their IE to a real browser. While any significant number of people still use IE, the course of human progress will be measurably slowed. – Michael Lorton Sep 13 '11 at 11:55
  • @Malvolio or the IE users just do not go back to your site ;) – epascarello Sep 13 '11 at 17:08
  • @epascarello -- that's the best part! The leakage isn't traceable back to my site. They visit my site, and a dozen others, and then an hour or two later, their IE does what IE does and crashes. My hands are clean! [pinkie finger to corner of mouth] Mwa-ha-ha-ha! – Michael Lorton Sep 13 '11 at 19:00
4

It´s a property. You can use it like this:

console.log(human.fullName); //Saeed Neamati

It´s a function that is called when accessing this property, and returns the value.

There are also setters available:

var human = 
{
   firstName: 'Saeed',
   lastName: 'Neamati',
   get fullName() {
       return this.firstName + ' ' + this.lastName;
   }
   set fullName(val) {
       var parts = val.split(' ');
       this.firstName = parts[0];
       this.lastName = parts[1];
   }
}

human.fullName = "Henry Miller";

But as cool as it might be, it´s not supported by all browsers. So it might be better avoid using it.

Van Coding
  • 24,244
  • 24
  • 88
  • 132