将“this”关键字发送到对象中

Sending "this" keyword into an object

本文关键字:对象 关键字 this      更新时间:2023-09-26
var View = Backbone.View.extend({
    id: 'foobar',
    param: {
        x: this.id // Will be "undefined"
    }
});

有没有办法利用this,可以使this.param.x指向,或者与this.id具有相同的价值?

不能在静态声明中使用对象的this。 可以在方法调用或使用代码的构造函数中设置该值,但不能在静态声明中设置该值。

使用类似的东西,

var View = Backbone.View.extend({
  id: 'foobar',
  initialize: function () {
      this.param = { x: this.id };
  }
});