Javascript 奇怪的视图属性导致主干

Javascript 奇怪的视图属性导致主干,javascript,backbone.js,Javascript,Backbone.js,我在主干中有一个视图,我想创建一些属性 我做到了: var ProgrammeDetailsView = Backbone.View.extend({ $infoResult: $('#info-result'), $castingResult: $('#casting-result'), $broadcastResult: $('#broadcast-result'), $testResult: $('#test'), [...] 我意外地得到了这个结果:

我在主干中有一个视图,我想创建一些属性

我做到了:

var ProgrammeDetailsView = Backbone.View.extend({

    $infoResult: $('#info-result'),
    $castingResult: $('#casting-result'),
    $broadcastResult: $('#broadcast-result'),
    $testResult: $('#test'),

[...]
我意外地得到了这个结果:

 console.log(this.$infoResult); -> Array { selector="#test", forEach=forEach(), reduce=reduce(), more...}
如您所见,我使用了“this.$infoResult”,它应该返回一个id为“#info result”的对象,但实际上它是返回的最新变量

如果我这样做:

var ProgrammeDetailsView = Backbone.View.extend({

    $infoResult: '#info-result',
    $castingResult: '#casting-result',
    $broadcastResult: '#broadcast-result',
    $testResult: '#test',
我没有问题,cf:

console.log(this.$infoResult); -> "#info-result"

你知道为什么吗?

你确定你所指的元素在初始化对象时可用吗?很多时候,可能正在创建此对象的视图或包含它的HTML可能尚未修改DOM,因此您将无法获得对它的引用

你完全正确@JohnP,我的错。元素此时不存在,我需要在呈现模板后调用它们。render:function(){this.$el.html(this.template(this.model.toJSON());this.$infoResult=$('#info result');this.$nextProgrammesResult=$('#next programmes result');this.$livetweet result=$('#livetweet result');console.log(this);