我刚刚学习了es 6而且我刚刚在...运行此代码时... ...
var name = [" Rahul"," Ritika"," Amit"," Radhika"]; 的console.log(name.length);
答案是25 .. 25为什么? 它必须有答案4但是给出25我不知道为什么?
我只是在浏览器控制台中运行代码答案是相同的...... 25
但当我将变量名称的名称更改为" c_name"它工作得很好......
让c_name = [" Rahul"," Ritika"," Amit"," Radhika"]; 的console.log(c_name.length);
当我试图执行此代码时,out再次错过了行为......
//姓名变量让名字= [" Rahul"," Ritika"," Amit"," Radhika"]; for(let i = 0; i< name.length; i ++)console.log(name [i]);
****output****
"Rahul"
"Ritika"
"Amit"
"Radhika"
//其他变量让c_name = [" Rahul"," Ritika"," Amit"," Radhika"]; for(let i = 0; i< c_name.length; i ++)console.log(c_name [i]);
的 输出
"R"
"a"
"h"
"u"
"l"
","
"R"
"i"
"t"
"i"
"k"
"a"
","
"A"
"m"
"i"
"t"
","
"R"
"a"
"d"
"h"
"i"
"k"
"a"
答案 0 :(得分:1)
这是因为name is a property on window object
。
如果您运行的代码不是全局范围,它将为您提供正确的结果。以下代码示例显示了如何使用IIFE (Immediately Invoked Function Expression)
:
(function(){
var name = ["Rahul","Ritika","Amit","Radhika"];
console.log(name.length);
})();