There seems to be an overlap in functionality between inline, object literal 'get function()' style and Object.defineProperty.
MDN docs for get https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get don't mention that inline 'get' functions are deprecated.
var john = {
firstName: 'John',
lastName: 'Smith',
age: 21,
gender: 'Male'
// () → String
// Returns the full name of object.
get name() {
return this.firstName + ' ' + this.lastName
},
// (new_name:String) → undefined
// Sets the name components of the object,
// from a full name.
set name(new_name) {
var names = new_name.trim().split(/\s+/)
this.firstName = names['0'] || ''
this.lastName = names['1'] || ''
},
}
This article from Mozilla's Jeff Walden in (what seems to be) 2010 stated:
"We’ve removed support for a handful of obsolete getter/setter syntaxes in SpiderMonkey and Mozilla. This does not include { get property() { return "value"; }, set property(v) { } }, which is widely used and which is part of the latest standard."
So:
- Is inline get/set OK?
- Is inline get/set deprecated in favor of defineProperty?
- When should I use each?