我正在学习 Backbone.js。我有一个模型调用 myModel,这是我调用 myModel.toJSON() 时的 console.log 结果:
Object {accountId: "523f628e80d52a2805000004", added: "2013-09-26T06:26:12.765Z", updated: "2013-09-26T06:26:12.765Z", _id: "5243d384951b6cef05000004", name: Object}
我想获取名称中的属性,当我 console.log myModel.toJSON().name 时,我得到了这个结果:
Object {first: "myfirst", last: "mylast"}
一切正常,直到我调用 myModel.toJSON().name.first,我得到一个控制台错误说
Cannot read property 'first' of undefined
我认为 myModel.toJSON().name 是一个对象,要从中获取“第一个”属性,我只需要执行 myModel.toJSON().name.first。任何想法为什么它不起作用?
这是我拥有的整个模型:
r {cid: "c71", attributes: Object, collection: r, _changing: false, _previousAttributes: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
_id: "5243d384951b6cef05000004"
accountId: "523f628e80d52a2805000004"
added: "2013-09-26T06:26:12.765Z"
name: Object
first: "myfirst"
last: "mylast"
__proto__: Object
updated: "2013-09-26T06:26:12.765Z"
__proto__: Object
changed: Object
cid: "c71"
collection: r
__proto__: s