Chrome Developer Tools Invoke Property Getter

元气小坏坏 提交于 2019-12-04 16:43:18

问题


All of my property values require me to click them in order to see them. How can I fix this?

The object I'm trying to view is this Query Object. It seems to do this with most Arcgis objects I'm trying to view.


回答1:


The issue is, calling a getter can have side effects e.g.

class Dog {
  get paws() {
    console.log('paws!'); //side effect
    this.paws++; // side effect
    if(this.paws > 4) {
     throw Error('oh no'); // side effect
    }

    return this.paws;
  }
}

Every getter can alter the state of the app or break it while you are trying to debug it. That's why DevTools ask you to invoke these getters manually. Even if your getter returns a static value, DevTools have no way of knowing that.

If you really want to invoke all getters and have a quick overview of the values, you can create yourself a helper:

class Dog {
 get _debug() {
  return {
    paws: this.paws,
    //...
  };
 }
}

This will add a new getter that will invoke all other getters for you and give you their values with a single click (instead of n clicks).




回答2:


You can try putting it through JSON stringify which will call all the getters:

console.log(JSON.parse(JSON.stringify(myObj)));



来源:https://stackoverflow.com/questions/45968614/chrome-developer-tools-invoke-property-getter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!