Arki 的每日 BUG 观察
41 subscribers
78 photos
1 video
3 files
228 links
分享每天写的bug
以前端为主
Download Telegram
#js
js 的默认排序是按照字母序递增排序,即使数组中全都是数字
[2, 10, 1].sort() // [1, 10, 2]
// 按数字大小排序
[2, 10, 1].sort((x, y) => x - y) // [1, 2, 10]
#js #tips
# js 数组去重
js 数组本身没有带去重的函数
可以用一个简单的函数实现
const array = [1, 1, 2]
// 多次使用
Array.prototype.unique = function() {
return this.filter( (value, index, self) => {
return self.indexOf(value) === index
})
}
array.unique()
// 单次使用
array.filter((v, i, arr) => arr.indexOf(v) === i)
#python #tips
# python list 去重
py 数组同样没有自带去重函数,但它的去重更简单
#  先将数组转为无重复元素的 set 再转回 list
list(set(array)) # uniq
#js
js 函数默认参数允许在没有值或 undefined 被传入时使用默认形参
const test =  (a=1) => a
test() // 1
test(null) // null
test(undefined) // 1
#tips #js
// map 返回默认值
const map = new Map ([
[1, 'foo'], // default
[2, 'bar'],
[3, 'baz'],
]);

const mapProxy = new Proxy(map, {
get: function(target, id) {
// Cast id to number:
id = +id;
return target.has(id) ? target.get(id) : target.get(1);
},
});
console.log( mapProxy[3] ); // baz
console.log( mapProxy[10] ); // foo

references:https://stackoverflow.com/questions/36947150/how-to-return-a-default-value-from-a-map
#tips #js
// 快速创建多维数组
const arr = new Array(i).fill(0).map(a => new Array(j).fill(0))
#git
git 默认对文件名大小写不敏感,因此在项目中修改文件名大小写时务必谨慎(已经重命名了的可能需要删除后重新添加
// 配置git对大小写敏感
git config core.ignorecase false
#好文推荐
wjndante的“概率与随机”三部曲之其一——如何用计算机生成“真随机”
http://bbs.ngacn.cc/read.php?tid=8477978&_fp=6
Forwarded from 荔枝木
🤣 JavaScript 的圣三位一体
#js #redux
action的枚举性质很适合使用es6的 Symbol ,但不推荐这么做,因为symbol无法序列化导致损失了redux的一些性质,比如无法把整个状态序列化保存到localStorage后无损还原回来,一些扩展工具如redux-devtools-extension(可通过参数手动支持)也因此(?)没有支持symbol.
因此在redux的action中请使用字符串,同时在redux中也请尽可能避免使用无法序列化的数据结构

相关讨论
[redux | Action types as symbols](https://github.com/reduxjs/redux/issues/4)
#js 对字符串进行运算前,将字符串转换成数字是个好习惯
#js trim
js 自带的 trim 函数是没有参数的,只能移除 white space and line terminator characters (空格和换行) 因此需要移除其他自定符号时请使用工具库的 trim 函数 (如lodash的 _.trim([string=''], [chars=whitespace]) )

'abc\n'.trim('a') // 'abc'
_.trim('abc\n', 'a') // 'bc\n'
#python
http://blog.jobbole.com/42706/
Python 新手常犯错误(第一部分)
TL;DR
不要用一个可变的值作为默认值
>>> def foo(numbers=[]):
... numbers.append(9)
... return numbers
>>> foo()
[9]
>>> foo()
[9, 9]
>>> foo()
[9, 9, 9]

正确做法
def foo(numbers=None):
if numbers is None:
numbers = []
numbers.append(9)
return numbers
如果变量是一个布尔值,变量名最好加上 is、has 或 can 作为前缀https://twitter.com/samantha_ming/status/1043578525339418624
#bug ASCII表中英文字母是不连续的,大写字母跟随着几个符号然后才是小写字符,因此判断字符是否英文字符不能使用 `c >= 'A' && c <= 'z'` 应该使用 `(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')` 或者使用正则表达式判断 `[A-Za-z]`