为什么每个JS对象都有一个未定义的属性

时间:2015-07-26 12:55:26

标签: javascript console undefined console.log

let obj = {a:'a',b:'b'};
for (let p in obj){
   console.log(p);
}

输出:

a 
b
undefined

未定义的属性是什么?

1 个答案:

答案 0 :(得分:3)

strong>打印。

示例:

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