观察者:我应该使用传递给函数的值,还是可以安全地使用 this.value 代替?

Observers: should I use values passed to the function, or can I use this.value safely instead?

我的应用程序中有这样的代码:

  observers: [
    '_dataGenericObserver(userData.generic)',
  ],

  // If this.userData.generic.id is set, then the register tab is
  // NOT visible. In this case, the selected tab mustn't be "register"...
  _dataGenericObserver: function(){
    if (this.userData.generic.id){
      this.selected = 'login';
    } else {
      if (this.defaultRegister) {
        this.selected = 'register';
      } else {
        this.selected = 'login';
      }
    }
  },

这里安全吗?或者我应该 总是 这样做:

  observers: [
    '_dataGenericObserver(userData.generic)',
  ],

  // If this.userData.generic.id is set, then the register tab is
  // NOT visible. In this case, the selected tab mustn't be "register"...
  _dataGenericObserver: function(generic){
    if (generic.id){
      this.selected = 'login';
    } else {
      if (this.defaultRegister) {
        this.selected = 'register';
      } else {
        this.selected = 'login';
      }
    }
  },

……? 我注意到在某些情况下,在应用程序的其他部分,如果我在 _someFunc(value.subvalue) 上有一个观察者,然后在 _someFunc: function( subValue) 上设置了函数 subValue,而 this.value.subValue不是。但是,我无法复制它 - 抱歉。

问题:

AFAIK 你应该总是明确地声明和使用你想要在观察者中访问的依赖项而不是使用 this 因为当你遇到 this.myVar 时可能不会设置 myVar 观察者函数的参数是。

至少官方 response from Polymer developers.(there might be some more issues 对此是这样的)

这适用于 Polymer 1.x 不确定这是否适用于 Polymer 2.x