Actually no, the one version is the new shorthand in es6
var someObject = {
get() {
}
};
which is actually pretty much the same as
var someObject = {
get: function() {
}
};
they are both method definitions, it's just part of the new shorthand notation
Update
I updated the answer a bit after seeing your screenshot. The difference between the getter/setters for p
& s
are because in p
they are added through the object initializer
, and in s
they are added through the Object.defineProperty
method.
I added a small example which should give you a similar output inside the console as the screenshot you were showing (provided you run it on chrome, on firefox it looks slightly different ;) )
// property getter/setter through object initializer
var p = {
_x: 5,
get x() {
return this._x;
},
set x(v) {
this._x = v;
}
};
// get/set function through defineProperty
var s = { _y: 10 };
Object.defineProperty( s, 'y', {
get() {
return this._y;
},
set(v) {
this._y = v;
}
});
p.x = 10;
s.y = 20;
console.log(p);
console.log(s);