Suppose I declare a structured Javascript variable like this:
假设我声明了一个结构化的Javascript变量,如下所示:
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
,'summary':this.list[1]['lname']
}
Question: How can I ensure that
问题:我如何确保这一点
this.list[1]['lname']
equals 'wilde' and that my "this" is actually referring to what I intend?
等于'wilde'而我的“这个”实际上指的是我的意图?
2 个解决方案
#1
You could also make this work in a function.
你也可以在一个函数中使这个工作。
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
,'summary':function(){return this.list[1]['lname']}
}
alert(mydata.summary());
#2
this won't work as written, but you could do:
这不会像书面那样有效,但你可以这样做:
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
}
mydata.summary = mydata.list[1]['lname'];
#1
You could also make this work in a function.
你也可以在一个函数中使这个工作。
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
,'summary':function(){return this.list[1]['lname']}
}
alert(mydata.summary());
#2
this won't work as written, but you could do:
这不会像书面那样有效,但你可以这样做:
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
}
mydata.summary = mydata.list[1]['lname'];