Javascript Object Literal referring to another property in itself from another property

后端 未结 3 1340
执笔经年
执笔经年 2020-12-07 02:42

I have a object literal:

var obj = {
    a : document.getElementById(\"ex1\"),
    b : obj.a.document.getElementsByTagName(\"div\")
};

I am

3条回答
  •  时光说笑
    2020-12-07 02:57

    The modern way to do this is with getter methods:

    let obj = {
      firstName: "A’dab",
      lastName: "Farooqi"
      get fullName() {
        return this.firstName+" "+this.lastName;
      },
    }
    

    So now you can just write obj.fullName - no need for the parentheses on the end.

提交回复
热议问题