Current object property as value in same object different property

前端 未结 3 1109
暗喜
暗喜 2020-12-22 03:48

Sorry for such a random title, but have no idea how to explain it better. And therefore, no idea if this is a duplicate question or not.

So, when declaring a new ob

3条回答
  •  情歌与酒
    2020-12-22 04:05

    In Javascript 1.5 you can use the get keyword to define a getter

    var obj = {
        super : 1,
        mega : 5,
        uber : 100,
        get giga() {
            return this.super + this.mega + this.uber;
        }
    };
    
    alert(obj.giga) // 106
    

    more on this http://robertnyman.com/2009/05/28/getters-and-setters-with-javascript-code-samples-and-demos/

提交回复
热议问题