/ 11 分钟阅读
本文最后更新于 2455 天前,文中所述的信息可能已发生改变或更新。
距离同系列上一篇已经一年了…还是要惊叹时间过得是如此之快。在对前端开发熟悉之后,对“坑”的定义也发生了变化,所以记录的反而少了,留下的都是些比较实用的方法。现在看回来,今年踏出的不寻常的一步是接触了 RN。RN 这个东西…我对他的心情还是挺复杂的。他确实给前端工程师提供了一个方便编写安卓、iOS 应用的方法,但是对于一些奇葩需求还是需要自己对接原生模块。而且我刚进坑拉下来的第一个版本就连最简单的 button 组件(没记错的话)都是有 bug 的 😂。至于未来,多了 flutter 这个竞争者,希望可以和 RN 进行一下良性竞争?
npm i -g npm-check-updates
ncu -u --packageFile package.json
apt-get 会安装假的 nodejs,真·安装方法:https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributionsgit+ssh://git@github.com:npm/cli.git#v1.0.27
git+ssh://git@github.com:npm/cli#semver:^5.0
git+https://isaacs@github.com/npm/cli.git
git://github.com/npm/cli.git#v1.0.27
"scripts": {
"install": "gulp build",
"test": "mocha --require babel-core/register ./test/**/*.js",
"lint": "eslint src/**/*.js"
},
<a href="download.url" download="filename">下载</a> 为 a 加上 download 属性浏览器就会下载链接的文件而不是在新页面打开。<iframe> 等标记中展现。<select>
<option value="" selected disabled hidden>请选择</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
必须在 SVG 名称空间中创建 SVG 元素,因此不能由 createElement 创建,而必须使用 createElementNS 将 SVG 名称空间作为第一个参数
抽取 object 中个别属性
const picked = (({ a, c }) => ({ a, c }))(object);
不能在 forEach 使用 await,forEach 要求同步函数。机制:async 函数返回一个 promise,最终是调用者处理这个 promise。然而,Array#forEach 不会处理回调函数返回的 promise,只是简单无视了他。
如果你需要顺序执行,可以这样
async function printFiles() {
const files = await getFilePaths();
for (const file of files) {
const contents = await fs.readFile(file, "utf8");
console.log(contents);
}
}
// 如果你需要并行执行,可以这样
async function printFiles() {
const files = await getFilePaths();
await Promise.all(
files.map(async (file) => {
const contents = await fs.readFile(file, "utf8");
console.log(contents);
}),
);
}
for/of 是遍历数组最可靠的方式,它比 for 循环简洁,并且没有 for/in 和 forEach()那么多奇怪的特例。for/of 的缺点是我们取索引值不方便,而且不能这样链式调用 forEach().forEach()。来源
encodeURIComponent 不处理
A-Z a-z 0-9 - _ . ! ~ * ' ( )
encodeURI 不处理
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
2011-10-05T14:48:00.000Z 这个格式很常见,这是 ISO 8601 标准,规定日期和时间中间用 T 连接,hh:mm:ss.sss 和 hh:mm:ss 都正确。JS 获取此格式可以使用 Date.prototype.toISOString()。然而,转换得来的是 0 时区的时间,如果要得到中国(+8)时间需要new Date(+new Date() + 8 * 60 * 60 * 1000).toISOString()
这是一个隐性坑:使用 x||y 一定要注意前面如果是 0 的情况:因为你获取数据成功,但是得到的数据 x 若刚好是 0 就会变成用后面的 y 了。
一句话解决一个经典入门难题:匿名函数的 this 指向 window,如果不是匿名,this 关键字永远都指向函数(方法)的所有者,如果使用了箭头函数,是外层函数的 this
ES7 语法 ::foo.bar 等于 foo.bar.bind(foo)
const noop = () => {}; 空操作函数,需要回调但无操作时使用
chrome 控制台有一个 copy() 方法,可以用于把数据复制到剪贴板,十分有用,知道以后就不用拿 node 写数据流了。对于对象可以这么写 copy(JSON.stringify(temp6))
babel 会把 es6 import 转为 commmonjs,启用 tree shaking 必须 es6,所以需要使用 tree shaking 时必须关闭 babel 的模块语法转换。
节流防抖的定义:
naturalWidth 和 naturalHeight 属性可以直接获取图片节点的原始宽高,且浏览器普遍支持。
数组也可以用 __proto__ 继承
a = [];
a.__proto__ = [2, 1, 2, 3, 3, 322];
a[1];
// 返回 1
>> 的使用:n 右移 1 位等于 n/2,右移两位等于 n/4,类推;左移则是相反;同时左右移可以用于去除小数
keep-alive 组件的 include 和 exclude 用的 name 是组件 name 而非路由 name。Animated 在安卓尽可能添加 useNativeDriver
Animated.timing(this.state.animatedValue, {
toValue: 1,
duration: 500,
useNativeDriver: true, // <-- Add this
}).start();
alignSelf: 'flex-start'// 定义
BigButton.ios.js;
BigButton.android.js;
// 引入时
import BigButton from "./BigButton";
page {
background-color: #f5f5f5;
}
backgroundColor<text> 标签的换行符会如实反映,看起来像多了 paddingexport PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 拯救命令丢失/ 11 分钟阅读
本文最后更新于 2455 天前,文中所述的信息可能已发生改变或更新。
距离同系列上一篇已经一年了…还是要惊叹时间过得是如此之快。在对前端开发熟悉之后,对“坑”的定义也发生了变化,所以记录的反而少了,留下的都是些比较实用的方法。现在看回来,今年踏出的不寻常的一步是接触了 RN。RN 这个东西…我对他的心情还是挺复杂的。他确实给前端工程师提供了一个方便编写安卓、iOS 应用的方法,但是对于一些奇葩需求还是需要自己对接原生模块。而且我刚进坑拉下来的第一个版本就连最简单的 button 组件(没记错的话)都是有 bug 的 😂。至于未来,多了 flutter 这个竞争者,希望可以和 RN 进行一下良性竞争?
npm i -g npm-check-updates
ncu -u --packageFile package.json
apt-get 会安装假的 nodejs,真·安装方法:https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributionsgit+ssh://git@github.com:npm/cli.git#v1.0.27
git+ssh://git@github.com:npm/cli#semver:^5.0
git+https://isaacs@github.com/npm/cli.git
git://github.com/npm/cli.git#v1.0.27
"scripts": {
"install": "gulp build",
"test": "mocha --require babel-core/register ./test/**/*.js",
"lint": "eslint src/**/*.js"
},
<a href="download.url" download="filename">下载</a> 为 a 加上 download 属性浏览器就会下载链接的文件而不是在新页面打开。<iframe> 等标记中展现。<select>
<option value="" selected disabled hidden>请选择</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
必须在 SVG 名称空间中创建 SVG 元素,因此不能由 createElement 创建,而必须使用 createElementNS 将 SVG 名称空间作为第一个参数
抽取 object 中个别属性
const picked = (({ a, c }) => ({ a, c }))(object);
不能在 forEach 使用 await,forEach 要求同步函数。机制:async 函数返回一个 promise,最终是调用者处理这个 promise。然而,Array#forEach 不会处理回调函数返回的 promise,只是简单无视了他。
如果你需要顺序执行,可以这样
async function printFiles() {
const files = await getFilePaths();
for (const file of files) {
const contents = await fs.readFile(file, "utf8");
console.log(contents);
}
}
// 如果你需要并行执行,可以这样
async function printFiles() {
const files = await getFilePaths();
await Promise.all(
files.map(async (file) => {
const contents = await fs.readFile(file, "utf8");
console.log(contents);
}),
);
}
for/of 是遍历数组最可靠的方式,它比 for 循环简洁,并且没有 for/in 和 forEach()那么多奇怪的特例。for/of 的缺点是我们取索引值不方便,而且不能这样链式调用 forEach().forEach()。来源
encodeURIComponent 不处理
A-Z a-z 0-9 - _ . ! ~ * ' ( )
encodeURI 不处理
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
2011-10-05T14:48:00.000Z 这个格式很常见,这是 ISO 8601 标准,规定日期和时间中间用 T 连接,hh:mm:ss.sss 和 hh:mm:ss 都正确。JS 获取此格式可以使用 Date.prototype.toISOString()。然而,转换得来的是 0 时区的时间,如果要得到中国(+8)时间需要new Date(+new Date() + 8 * 60 * 60 * 1000).toISOString()
这是一个隐性坑:使用 x||y 一定要注意前面如果是 0 的情况:因为你获取数据成功,但是得到的数据 x 若刚好是 0 就会变成用后面的 y 了。
一句话解决一个经典入门难题:匿名函数的 this 指向 window,如果不是匿名,this 关键字永远都指向函数(方法)的所有者,如果使用了箭头函数,是外层函数的 this
ES7 语法 ::foo.bar 等于 foo.bar.bind(foo)
const noop = () => {}; 空操作函数,需要回调但无操作时使用
chrome 控制台有一个 copy() 方法,可以用于把数据复制到剪贴板,十分有用,知道以后就不用拿 node 写数据流了。对于对象可以这么写 copy(JSON.stringify(temp6))
babel 会把 es6 import 转为 commmonjs,启用 tree shaking 必须 es6,所以需要使用 tree shaking 时必须关闭 babel 的模块语法转换。
节流防抖的定义:
naturalWidth 和 naturalHeight 属性可以直接获取图片节点的原始宽高,且浏览器普遍支持。
数组也可以用 __proto__ 继承
a = [];
a.__proto__ = [2, 1, 2, 3, 3, 322];
a[1];
// 返回 1
>> 的使用:n 右移 1 位等于 n/2,右移两位等于 n/4,类推;左移则是相反;同时左右移可以用于去除小数
keep-alive 组件的 include 和 exclude 用的 name 是组件 name 而非路由 name。Animated 在安卓尽可能添加 useNativeDriver
Animated.timing(this.state.animatedValue, {
toValue: 1,
duration: 500,
useNativeDriver: true, // <-- Add this
}).start();
alignSelf: 'flex-start'// 定义
BigButton.ios.js;
BigButton.android.js;
// 引入时
import BigButton from "./BigButton";
page {
background-color: #f5f5f5;
}
backgroundColor<text> 标签的换行符会如实反映,看起来像多了 paddingexport PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 拯救命令丢失