Javascript 为什么';对象点符号在for…in循环中工作?
这项工作:Javascript 为什么';对象点符号在for…in循环中工作?,javascript,oop,object,iteration,for-in-loop,Javascript,Oop,Object,Iteration,For In Loop,这项工作: car = { color: 'green', speed: '340', drive: function() { alert("brrrrrrm"); } } 但这不起作用,返回每个元素的未定义值: for(elem in car) { console.log(car[elem]); } 编写car.elem时,您试图访问car对象的elem属性,该属性不存在 但是当您使用car[elem]时,您试图访问car对象的属性,该属性的名称与存储在变量elem中的
car = {
color: 'green',
speed: '340',
drive: function() {
alert("brrrrrrm");
}
}
但这不起作用,返回每个元素的未定义值:
for(elem in car) {
console.log(car[elem]);
}
编写
car.elem
时,您试图访问car对象的elem
属性,该属性不存在
但是当您使用car[elem]
时,您试图访问car对象的属性,该属性的名称与存储在变量elem中的名称相同
因此,如果使用“点表示法”,最终将调用car对象的elem属性
for(elem in car) {
console.log(car.elem);
}
访问属性:
car = {
color: 'green',
speed: '340',
elem: 'This is the elem property of car object',
drive: function() {
alert("brrrrrrm");
}
}
控制台输出:
for(elem in car) {
console.log(car[elem]);
}
for(elem in car) {
console.log(car.elem);
}
然后我们做:
green
340
This is the elem property of car object
function () {
alert("brrrrrrm");
}
控制台输出:
for(elem in car) {
console.log(car[elem]);
}
for(elem in car) {
console.log(car.elem);
}
编写
car.elem
时,您试图访问car对象的elem
属性,该属性不存在
但是当您使用car[elem]
时,您试图访问car对象的属性,该属性的名称与存储在变量elem中的名称相同
因此,如果使用“点表示法”,最终将调用car对象的elem属性
for(elem in car) {
console.log(car.elem);
}
访问属性:
car = {
color: 'green',
speed: '340',
elem: 'This is the elem property of car object',
drive: function() {
alert("brrrrrrm");
}
}
控制台输出:
for(elem in car) {
console.log(car[elem]);
}
for(elem in car) {
console.log(car.elem);
}
然后我们做:
green
340
This is the elem property of car object
function () {
alert("brrrrrrm");
}
控制台输出:
for(elem in car) {
console.log(car[elem]);
}
for(elem in car) {
console.log(car.elem);
}
你有叫“elem”的钥匙吗?不,这就是为什么。@GerardoFurtado该死的,那是快速准确的,谢谢!也许能给我一些启发。“BRRRM”部分让我笑了你有叫“elem”的钥匙吗?不,这就是为什么。@GerardoFurtado该死的,那是快速准确的,谢谢!也许能给我一些启发。“BRRRM”部分让我笑了