Arki 的每日 BUG 观察
#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,…
es6还可以使用
可以的话更推荐使用工具库的去重函数 如 lodash的
Set 去重
uniq = arr => [...new Set(arr)]
可以的话更推荐使用工具库的去重函数 如 lodash的
_.uniq(arr) 因为一些特殊的数值如 NaN 不好处理https://medium.com/javascript-non-grata/the-top-10-things-wrong-with-javascript-58f440d6b3d8
#js
The Top 10 Things Wrong with JavaScript
#js
The Top 10 Things Wrong with JavaScript
Medium
The Top 10 Things Wrong with JavaScript
JavaScript has a reputation for being one of the worst programming languages in existence, and for good reasons! JavaScript is easy to…
#js #redux
action的枚举性质很适合使用es6的
因此在redux的action中请使用字符串,同时在redux中也请尽可能避免使用无法序列化的数据结构
相关讨论
[redux | Action types as symbols](https://github.com/reduxjs/redux/issues/4)
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)
GitHub
Action types as symbols · Issue #4 · reduxjs/redux
Allowing this would be awesome. Why did you enforce type as string? (createDispatcher.js#L67) export const INCREMENT_COUNTER = Symbol() export const DECREMENT_COUNTER = Symbol()
#js trim
js 自带的
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
不要用一个可变的值作为默认值
正确做法
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 也可能会触发
-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 exceptionForwarded from YSC 的频道
C++11 开始,std::string 可以直接用作缓冲区,不需要用另外一块空间保存,然后再拷贝到 std::string 中。
https://stackoverflow.com/a/39200666
在 C++11/14 中可以使用
https://stackoverflow.com/a/39200666
在 C++11/14 中可以使用
&str.front() 或 &str[0] 来获取地址,C++17 中 str.data() 加入了非 const 的重载,可以直接用 str.data() 来获取地址。str.size() 是缓冲区的长度,可以用 str.resize(length) 分配空间。Stack Overflow
Directly write into char* buffer of std::string
So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data() return const char*, I can't use them. So I was allocating a tem...
#js
因此查询普通字符串的时候,请使用`string.indexOf`
'([{'.search('(')
// Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
string.search 字符串的查询方法参数是一个 `RegExp 如果给的是字符串,会被强制转换为 RegExp因此查询普通字符串的时候,请使用`string.indexOf`
#js
判断空数组请不要使用
请使用
判断空数组请不要使用
=== 直接与空数组比较[] === [] // false请使用
if (Array.isArray(array) && array.length === 0) {
// empty array
}#js
js string有两个substr相关的方法
两个方法的去别在于参数不同
string.substr takes parameters as (from, length).
string.substring takes parameters as (from, to).
js string有两个substr相关的方法
string.substr 和 string.substring 两个方法的去别在于参数不同
string.substr takes parameters as (from, length).
string.substring takes parameters as (from, to).
console.log("abc".substr(1,2)); // returns "bc"
console.log("abc".substring(1,2)); // returns "b"#java
java应用打成jar包之后classpath下的resources文件不能用File的方式读取
java应用打成jar包之后classpath下的resources文件不能用File的方式读取
@Value("classpath:menu.json")
private Resource res;
// res.getFile(); ×
res.getInputStream();