ES6 学习笔记

2019-08-04 Sunday ES6

记录ES6 使用的一些语法知识

0. Node 开发环境配置

配置淘宝源,加速库下载

npm config set registry  "http://registry.npm.taobao.org/"
npm config set sass_binary_site  "https://npm.taobao.org/mirrors/node-sass/"
1
2

1. Babel

npm install -g babel-cli
npm install babel-preset-env --save-dev
1
2

2. request

let request = obj => {
    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();
        xhr.open(obj.method || "GET", obj.url);
        if (obj.headers) {
            Object.keys(obj.headers).forEach(key => {
                xhr.setRequestHeader(key, obj.headers[key]);
            });
        }
        xhr.onload = () => {
            if (xhr.status >= 200 && xhr.status < 300) {
                resolve(xhr.response);
            } else {
                reject(xhr.statusText);
            }
        };
        xhr.onerror = () => reject(xhr.statusText);
        xhr.send(obj.body);
    });
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

参考资料

3. moment

3.1 安装

npm install moment --save
1

3.2 格式转换

从timestamp到时间。

4. 字符串格式化

4.1 前补0

// format with prefix 0
const formattedOutput = output.map((line) => line.padStart(3, '0'));

console.log(formattedOutput); // example of what you can do with the formatted output

1
2
3
4
5

5. 执行命令

5.1 同步执行命令并返回多行结果

const spawn = require('cross-spawn')
// spawn.sync get result with new line 
const result = spawn.sync('command', ['arg1', 'arg2']);
const output = result.stdout.toString().split('\n');
1
2
3
4
最近更新: 2023-04-09 19:35:35