为什么每个 JS 对象都有一个 undefined 属性
Why does every JS object have one undefined property
let obj = {a:'a',b:'b'};
for (let p in obj){
console.log(p);
}
输出:
a
b
undefined
未定义的 属性 是怎么回事?
那不是 属性,它是函数 的 return 开发者工具打印。
示例:
console.logger = function(p){
console.log(p);
return "logged"
}
var obj = {a:'a',b:'b'};
for (var p in obj){
console.logger(p);
}
a // logs the property
b // logs the property
"logged" // logs the return of logger function, undefined in case nothing's returned
let obj = {a:'a',b:'b'};
for (let p in obj){
console.log(p);
}
输出:
a
b
undefined
未定义的 属性 是怎么回事?
那不是 属性,它是函数 的 return 开发者工具打印。
示例:
console.logger = function(p){
console.log(p);
return "logged"
}
var obj = {a:'a',b:'b'};
for (var p in obj){
console.logger(p);
}
a // logs the property
b // logs the property
"logged" // logs the return of logger function, undefined in case nothing's returned