3

My question is simple, how could i make aliases on object getters.

Example:

MyClass.prototype = {
    constructor: MyClass,
    get a() {
      // do stuff
    },
    get ab() {
      // do stuff
    },
    get abc() {
      // do stuff
    }
}

Here, a, ab and abc does exactly the same, but it is mandatory to have these 3 different getters, or more precisely, it is mandatory to have 3 different names over the same functionality.

get a = ab = abc {

}

is obviously not working, neither does

get a() = ab() = abc() {

}

Any suggestions?

Thanks

Jim-Y
  • 1,637
  • 4
  • 16
  • 31

2 Answers2

1
get ab(){
    return this.a;
}
kornieff
  • 2,259
  • 18
  • 29
1

Don't use an object literal, you cannot have a self-reference within one. Instead, define the properties programmatically - and you can indeed re-use the same property descriptor for each of them.

var desc = {
    configurable: true,
    get: function() {
      // do stuff
    }
};
Object.defineProperties(MyClass.prototype, {
    a: desc,
    ab: desc,
    abc: desc
});
Community
  • 1
  • 1
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • Thanks, finally i did this way: https://github.com/jim-y/checkjs/blob/master/lib/check.js#L209 – Jim-Y Sep 08 '14 at 10:46