1.循环
1 | // 1. for 普通循环 |
所以,应该优先使用for of,其次for 普通的循环。
2. slice splice
- slice 从数组返回选定的元素,比如浅拷贝slice(0),返回的也是一个数组。
arrayObject.slice(start,end)
- splice 对数组中添加/删除项目,然后返回被删除的项目。这个会修改原数组。
arrayObject.splice(index,howmany,item1,…..,itemX)
这两个都是返回被选定的from start to end,或者length长度的数组。不是返回修改后的数组,也不是参数。
2. Array 操作的坑
2.1 push 不需要重新赋值,不能使用返回值赋值
js 的push 注意返回值
1 | let arr = [1, 2] |
2.2 concat 需要重新赋值,必须使用返回值赋值
1 | const a = [1, 2] |
2.4 splice 不需要重新赋值,不能使用返回值赋值
1 | const b = arr.splice(1,1,3) // 错误, arr.splice(1,1,3) 返回的[2], 原位置的值 |
2.5 slice 需要重新赋值,必须使用返回值赋值
1 | const b = arr.slice(1,2) |
slice不修改原数组,splice修改原数组。还有一个区别,第二个参数:slice是下标,可以越界(因为它不包含),splice是长度,不可以越界。
3.null vs undefined
null, undefined都是primitive type.但是有个错误,typeof null === ‘object’。
1 | null == undefined // true |
- null表示一个属性定义了,但是有目的的设为null;undefined表示这个属性没有。
- 一般初始化时设为null,然后判断时直接用=== null判断有没有设置值。
- 函数参数要是没有实参,默认是undefined.
When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time
Summary
null is an assigned value. It means nothing.
undefined typically means a variable has been declared but not defined yet.
null and undefined are falsy values.
null and undefined are both primitives. However an error shows that typeof null = object.
null !== undefined but null == undefined.
https://codeburst.io/javascript-null-vs-undefined-20f955215a2
4. 三目运算赋初始值
我经常这么用 const a = b ? b : defaultValue
,这个其实不是很好的习惯。记住这里b的运算时及时布尔值,而可能我们需要的是undefined这种情况,像0,null,false这中我们给了值,但是是falsy的值,我们如果用了默认defaultValue,就会产生预料之外的情况,就可能有bug.
我后来觉得三目运算赋值比&&
赋值好些,&&
返回的可能是第一个值,更加难以觉察到bug。比如const a = b && String(b)
, 当b为0时,其实我希望是’0’,而这行代码返回0,就是bug。
1 | // !! 和 Boolean功能一样,但是React中用Boolean好一些,因为在jsx中用!!0会有bug. |
5. immutable List
Immutable.List和原生的数组是有区别,前者可以链式调用。而且splice还不修改原List,因为不可变的特性!
1 | const list = List.of('a', 'b', 'c', 'd') |
6. Object.values的key排序问题
一般来说HashpMap的遍历的顺序都是不确定的,但是JavaScript的顺序是确定的。如果是数字key那么就是按数字大小排序;如果是string,那么按照加入object的顺序排序;如果是Symbols,也是按照加入object的顺序排序。
Own Property Keys:
- Retrieves the keys of all own properties of an object, in the following order:
- First, the string keys that are integer indices (what these are is explained in the next section), in ascending numeric order.
- Then all other string keys, in the order in which they were added to the object.
- Lastly, all symbol keys, in the order in which they were added to the object.
- Used by: Object.assign(), Object.defineProperties(), Object.getOwnPropertyNames(), Object.getOwnPropertySymbols(), Reflect.ownKeys()
https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key/31102605#31102605
https://exploringjs.com/es6/ch_oop-besides-classes.html#_traversal-order-of-properties
7. Array
JavaScript的数组其实是个Object,它的key是数字值的string。JS只有6种类型,其中5中基本类型(boolean, number, string,null, undefined)和 object。
但是typeof null == ‘object’。这是一个坑。
1 | typeof null // 'object' |