gsklee
gsklee

Reputation: 4944

Sending "this" keyword into an object

var View = Backbone.View.extend({
    id: 'foobar',

    param: {
        x: this.id // Will be "undefined"
    }
});

Is there any way making use of this, that can make this.param.x pointing to, or has the same value with this.id?

Upvotes: 0

Views: 113

Answers (2)

Alexander Beletsky
Alexander Beletsky

Reputation: 19821

Use something like that,

var View = Backbone.View.extend({
  id: 'foobar',

  initialize: function () {
      this.param = { x: this.id };
  }
});

Upvotes: 1

jfriend00
jfriend00

Reputation: 707258

You can't use the object's this in a static declaration. You can set that value in a method call or in the constructor with code, but not in a static declaration.

Upvotes: 3

Related Questions