当前位置:网站首页>Record some pits encountered by JS
Record some pits encountered by JS
2022-04-22 07:47:00 【I'm just a code porter】
1.typeof: Undefined variables typeof return "undefined".
2. data type :
1)js in 0 == '' The return is true
2)1 == '1' The return is true, But in Map in 1 and '1' It's different
3) When obtaining input box data for calculation , Please convert to number first
4)undefined+ Any numerical result is NaN
5) character string +undefined The result is a string +'undefined' character string
3. Judge :
1)if(data), When data by 0 Or empty string ('') perhaps undefined when , Will not enter the judgment
2)if(!!data), For judgment true perhaps false however data It doesn't have to be true perhaps false
4. Easy to make mistakes :
1) Example 1: Because the function is not in console.info() Print it when you need it , Instead, save the function outside ,10 One function has the same scope , And are 10
var a = [];
for(var i=0; i<10; i++){
a[i] = function (){
console.info(i);
}
}
for(var i=0; i<a.length; i++){
// Output is normal , But after the loop is completed, the data is all 10
a[i]();
}
for(var k=0; k<a.length; k++){
// The output results are all 10
a[k]();
}
2) Example 2: because num Undefined ,num by undefined,undefined+ Any number is NaN
var num;
for(var i=1; i<=10; i++){
num = num+i;
}
// The output is NaN
console.info(num);
3) Example 3:return When the line of the statement has nothing else , Will be in return Automatically insert semicolon after statement
var f1 = function (){
return
{
test: 'test'
}
}
// The printout is undefined
console.info(f1());
版权声明
本文为[I'm just a code porter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220620280506.html
边栏推荐
猜你喜欢
随机推荐
Strong net cup 2019 random note
Questions d'entrevue de l'unit é
Unity基础知识
Problem D: 孪生素数
寻找3d地图上没有障碍物的点
Use use use
写第一个代码需要注意的
@ transactional transaction propagation in the same class
C#中的枚举和结构体
常用的异常
Unity 获取网络 并转换成时间戳
数据库高级查询
小程序 - canvas绘制海报
A thrilling redis vulnerability to the principle of server target changing
unityJson文件创建和读取
Pymol用法
JS取出两个数组中相同的元素
CameraFlyControllerEditor
Select structure (if switch)
第八章 异常处理和字符串处理和文件操作









