最近用因为用“name”做js数组类型变量名称时候发现个问题
html代码
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<script>
var name=["aa","bb","cc"];
var otherName=["aa","bb","cc"];
console.info(name);
console.info(otherName);
</script>
</head>
<body>
</body>
</html>
很明显,两个一模一样的赋值, 输出结果却完全不一样,一个是字符串,另一个是一个字符串数组
在很多时候,对前端的数据使用来说,是致命的,类型都不一样
难道只有数组类型的数据是这样的吗,接着,我把html代码改成这样:
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<script>
var name={
firstname : "Bill",
lastname : "Gates",
id : 5566
};
var otherName={
firstname : "Bill",
lastname : "Gates",
id : 5566
};
console.info(name);
console.info(otherName);
</script>
</head>
<body>
</body>
</html>
输出:
w t f ! 这是怎么回事,还是不一样!
然后,我又去查了一下:“name”并不是js的关键字(实际上关键字用于命名的话,会报错的
那,为什么会出现这个问题呢,真是百思不得其姐。
记下,以后命名需注意。
2018更新:
因为“name”是当前窗体:window对象的属性所致;