Javascript - 如何使 Object.property.property 成为有效的 属性?
Javascript - How do i make Object.property.property a valid property?
我目前正在上在线 javascript 课程,它说
Object.property.property 是一个有效变量,但我终其一生都无法在互联网上找到任何关于它的信息,也无法在我的程序中使用它。在本节的注释中,它说可以使对象的 属性 具有子 属性。这是示例引用的代码:
function car () {
this.weight=0;
this.engine="";
this.aero_factor=0;
this.speed=0;
}
var porsche = new car()
它在说"Properties can be Objects, too"时给出的例子是:
car.interiorStyle=”Type 12”;
car.interiorStyle.upholstery=”Leather”;
car.interiorStyle.airConditioning=true;
car.interiorStyle.radio=”JVC”;
car.interiorStyle.radio.power=200;
但仅此而已。它根本没有说明如何实现它,我不明白为什么当我这样做时,它每次都以 undefined 结束。
这是我的代码:
function Car(weight, speed, turboSpeed) {
this.weight=weight;
this.speed=speed;
this.speed.turbo = turboSpeed;
}
var porsche = new Car(1750, 125, 250);
alert(porsche.weight); //1750
alert(porsche.speed); //125
alert(porsche.speed.turbo); //undefined
如果我能得到一些帮助,那就太好了。谢谢!
你几乎是对的。当他们说 "also" 时,他们的意思是您可以像这样为他们分配一个对象:
this.speed={normal:55,turbo:95};
那么this.speed.normal==55
,然后this.speed.turbo==95
只要不在原始类型的属性上定义 属性,就可以随时定义 object.property.property
我目前正在上在线 javascript 课程,它说 Object.property.property 是一个有效变量,但我终其一生都无法在互联网上找到任何关于它的信息,也无法在我的程序中使用它。在本节的注释中,它说可以使对象的 属性 具有子 属性。这是示例引用的代码:
function car () {
this.weight=0;
this.engine="";
this.aero_factor=0;
this.speed=0;
}
var porsche = new car()
它在说"Properties can be Objects, too"时给出的例子是:
car.interiorStyle=”Type 12”;
car.interiorStyle.upholstery=”Leather”;
car.interiorStyle.airConditioning=true;
car.interiorStyle.radio=”JVC”;
car.interiorStyle.radio.power=200;
但仅此而已。它根本没有说明如何实现它,我不明白为什么当我这样做时,它每次都以 undefined 结束。
这是我的代码:
function Car(weight, speed, turboSpeed) {
this.weight=weight;
this.speed=speed;
this.speed.turbo = turboSpeed;
}
var porsche = new Car(1750, 125, 250);
alert(porsche.weight); //1750
alert(porsche.speed); //125
alert(porsche.speed.turbo); //undefined
如果我能得到一些帮助,那就太好了。谢谢!
你几乎是对的。当他们说 "also" 时,他们的意思是您可以像这样为他们分配一个对象:
this.speed={normal:55,turbo:95};
那么this.speed.normal==55
,然后this.speed.turbo==95
只要不在原始类型的属性上定义 属性,就可以随时定义 object.property.property