我想从同一对象文字中的另一个属性中引用对象文字中的嵌套属性。
考虑以下人为设计的示例:
var obj = {
product1: {
price: 80,
price_was: 100,
discount: function(){
return 100 - (100 * (price/price_was));
//I don't want to use:
//100 - (100 * (this.product1.price/this.product1.price_was))
//because the name of the parent ('product1' in this case) isn't known
//a-priori.
}
}
}
以上显然是不正确的,但是如何从'discount'中得到'price'和'price_was'呢?
我已经查看了以下问题,该问题很接近,但在该问题中,所需的属性是“this”的直接子级,在上面的示例中并非如此。 对象文字中的引用变量?
有什么办法可以做到这一点?