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

Attila Kling
  • 1,717
  • 4
  • 18
  • 32

2 Answers2

1
get ab(){
    return this.a;
}
kornieff
  • 2,389
  • 19
  • 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
  • 630,263
  • 148
  • 957
  • 1,375