javascript - Reference variable in definition? -
how can reference variable while define in javascript?
var person = { basic: { name: 'jack', sex: 0, }, profile: { aa: 'jack' + '_sth', # how can write this: aa: basic.name + '_sth' }, };
you can't.
you have do
var name = 'jack'; var person = { basic: { name: name, sex: 0 }, profile: { aa: name + '_sth' } }; just this answer says, following
function person() { this.basic = { name: 'jack', sex: 0 }; this.profile = { aa: this.basic.name + '_sth' }; } var person = new person(); but creates instance of person, not plain , simple js object.
Comments
Post a Comment