考虑以下代码:
function Animal(){
this.type = "dog";
this.color = {
stomach: "white",
paws: "brown",
face: function(){
if(this.color.stomach === "white"){
return "red";
}else{
return "blue";
}
}
}
这只颜色奇特的狗的脸色取决于他的胃的颜色。我想知道是否有一种语法更简单的方法来编写“this.color.stomach”部分。即,“this”指的是主要的 Animal 对象。是否有类似的关键字指向调用该关键字的父对象?例如,由于我已经在 Animal.color 中,而不必重复该部分来获得它的胃颜色(Animal.color.stomach),有没有办法直接引用颜色属性,这样它就可以像“parent.stomach”,其中“parent”指的是它被调用的任何属性——在这种情况下,Animal.color?