Arki 的每日 BUG 观察
41 subscribers
78 photos
1 video
3 files
228 links
分享每天写的bug
以前端为主
Download Telegram
#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]`
#c
-2147483648/-1
整数除以 0 可能会触发 SIGFPE,最小的 INT(-2147483648)除以 -1 也可能会触发
#include<stdio.h>
int main() {
int a = -2147483648; // 0x80000000
int b = -1;
int ans = a / b;
printf("%d\n", ans);
return 0;
}
运行结果 Floating point exception
Forwarded from YSC 的频道
C++11 开始,std::string 可以直接用作缓冲区,不需要用另外一块空间保存,然后再拷贝到 std::string 中。
https://stackoverflow.com/a/39200666
在 C++11/14 中可以使用 &str.front()&str[0] 来获取地址,C++17 中 str.data() 加入了非 const 的重载,可以直接用 str.data() 来获取地址。
str.size() 是缓冲区的长度,可以用 str.resize(length) 分配空间。
#js (日常黑js