-2

So yeah, is there a difference between get() {} and get: function() {} declaration in objects or this is completely two different things?

Actually I can't get get: function() {} to work even like a simple method, but as a getter too.

So maybe the question must sounds like: What the get: function() {} is?

Thanks.

UPD: http://take.ms/h6xII - here is of what I'm talking about.

almazmusic
  • 135
  • 1
  • 2
  • 8
  • 1
    Do you mean `get() {}` or `get property() {}`, since you're talking about getters? – Bergi May 31 '17 at 16:53
  • I suggest you read about scopes, functions and objects in JavaScript, you'll get your answer there. https://www.w3schools.com/js/js_objects.asp – P. Lalonde May 31 '17 at 16:55
  • 1
    When you say... "get: function()" are you making the declaration inside of a class? I'd recommend showing the two declaration styles in a complete example. – Erik Hermansen May 31 '17 at 16:56
  • 1
    @P.Lalonde that has nothing todo with scope, and i think the op is already aware of functions + objects in a neccessary way... ( and please dont recommend w3schools, they just give snippets while MDN includes explanation + advanced stuff) – Jonas Wilms May 31 '17 at 16:59
  • possible duplicate of [How does this object method definition work](https://stackoverflow.com/q/32404617/1048572)? – Bergi May 31 '17 at 17:00
  • No, its not a duplicate. ES5 have that notation for object methods too. And what is the difference between `get() {}` and `get property() {}`? – almazmusic May 31 '17 at 17:34
  • So as I understood `get: function named() {}` is completely different from getter and named function just able to call itself in recursion? Right? – almazmusic May 31 '17 at 17:56
  • @almazmusic You still didn't post any code. Even that screenshot you shared doesn't show how `p` and `s` are defined. – Bergi May 31 '17 at 18:49
  • 1
    @almazmusic And no, `get() {}` is an ES6 shorthand (roughly for `get: function() {}`). If that is not what are talking about, please remove it from your question. `get property() {}` is the ES5 getter syntax. – Bergi May 31 '17 at 18:51

2 Answers2

2

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);
Icepickle
  • 12,689
  • 3
  • 34
  • 48
0

I am assuming you are using the first example as function get() {} and the 2nd example as an object like

var obj = {
   get: function() {}
};

The only main difference between the above and

var obj = {
  get() {}
};

Is that the 2nd is a shorthand in es6 both are function expressions.

dhershman
  • 341
  • 2
  • 12