日常工作中遇到和文章中看到的一些易错点记录
1:如果代码中可能没有声明一个变量name
- 使用typeof name === ‘undefiend’ // 这样可以判断有没有声明
- 而不是if(name) // 会报错,因为name没有声明
2:setTimeout会返回一个timmerId变量
用来清除定时器,clearTimeout(timmerId); 清除定时器作用是阻止定时器任务执行,并不是把该timmerId置为null
3: 负margin
margin-left: -10px; 缩小距离左边的距离,意思是把自身向左移动10px;圣杯布局中会用到
4:伪元素的position:absolute时
如果他自身的元素有position:relative,或者absolute,或者fixed,则相对他自身的位置来定位
5:JavaScript中的所有内容都是 :原始值或对象
6:JavaScript中只有6个假值:
- undefined
- null
- NaN
- 0
- '' (empty string)
- false
注意:函数构造函数,如new Number和new Boolean都是真值。
7:对象作为object类型的key时
会自动转换为字符串化,它变成了
[Object object]
示例:
const a = {};
const b = { key: "b" };
const c = { key: "c" };
a[b] = 123;
a[c] = 456;
console.log(a[b]); // 456
8:如果对象有两个具有相同名称的键,则将替前面的键。它仍将处于第一个位置,但具有最后指定的值。
const obj = { a: "one", b: "two", a: "three" };
console.log(obj);
A: { a: "one", b: "two" }
B: { b: "two", a: "three" }
C: { a: "three", b: "two" }
D: SyntaxError
答案: C
9:{ age: 18 } == { age: 18 } 不相等
因为等号两边都在栈区创建了两个引用地址,两个引用地址不相等