JavaScript。无法理解为什么我的第一个控制台语句给出一个未定义的

时间:2022-01-19 20:20:30
function person(name){

   this.name1 = "His";

   this.day = {};
   this.day.time="kokok";


    this.check = function(name){

    for(var i in this)  console.log(this["i"]);

    if(this.name1===name) console.log("ready"+this.day.time);
     };
 }


 var t= new person("His");
 t.check("His");

1 个解决方案

#1


It needs to be:

它需要是:

 console.log(this[i]);

instead of:

 console.log(this["i"]);

You want to reference the variable i, not a string "i".

您想引用变量i,而不是字符串“i”。

#1


It needs to be:

它需要是:

 console.log(this[i]);

instead of:

 console.log(this["i"]);

You want to reference the variable i, not a string "i".

您想引用变量i,而不是字符串“i”。