Is there any real difference between the get operator:
var obj = {
get prop () {
//insert code here
}
};
and using defineProperty:
var obj;
Object.defineProperty(obj, "prop", {
get: function () {
//insert code here
}
}
The MDN pages say that compatibility is about the same.
Object.definePropertywill default toenumerable: falseandconfigurable: false, while the object literal getter syntax will default toenumerable: trueandconfigurable: true. This can be verified withObject.getOwnPropertyDescriptor(obj, "prop").This means that in the former case
propwill show up infor–inloops andObject.keys(obj), and doing adelete obj.propwill fail (noisily in strict mode, silently otherwise). The opposite will be true for the latter case.Note that
Object.defineProperty(orObject.createorObject.defineProperties) will allow you to separately choose the configurability and enumerability of your properties, while the object literal getter syntax will not.