Day learn,day up

前言

忽略我这个中文式英语的标题。

身为一个记性不咋地的前端渣渣,觉得平时看的一些文章太散了,特开此文作为一种记录,可谓好记性不如烂笔头,也算是逼自己要经常学习。文章的日期为最后更新时间,题目顺序不分先后,希望能经常保持置顶状态,目录到达一定长度时会重开一篇。

工具

hexo迁移

因为我的笔记本电脑最近瓦特了,所以不得不把博客迁出来,在这里做个记录,方便下次查找。

  • 将原来的电脑上原有的hexo目录拷贝到新电脑,只需拷贝以下目录:

    • _config.yml
    • package.json
    • scaffolds/
    • source/
    • themes/
  • 在新电脑安装hexo

    npm install -g hexo

  • 安装后进入hexo/目录

  • 安装模块

    1
    2
    3
    4
    npm install
    npm install hexo-deployer-git --save
    npm install hexo-generator-feed --save
    npm install hexo-generator-sitemap --save
  • 然后重新配置github

    1
    2
    git config --global user.name "YOUR NAME"
    git config --global user.email "YOUR EMAIL ADDRESS"

    Authenticating with GitHub from Git

    • 在 Git Bash 下执行如下命令,生成 SSH key

      ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

      email为自己的github账号,剩下的一路回车即可。

    • 将 SSH key 添加到 ssh-agent

      • 在 Git Bash 下执行如下命令,开启 ssh-agent

      eval "$(ssh-agent -s)"

      • 将这个 SSH key 添加到 ssh-agent 里去

      ssh-add ~/.ssh/id_rsa

    • 将 SSH key 添加到 Github 账户

      • 在 Git Bash 中将 SSH Key 拷贝出来:

      clip < ~/.ssh/id_rsa.pub

      此时,SSH Key 已经在我们的剪切板里了。然后登录 Github 帐号,依次点击自己的头像,SettingsSSH and GPG keysAdd SSH key, 在 Title 这里输入 Key 的label,比如 your_name - PC,然后在 Key 里面把 SSH Key 粘贴进去,点击 Add SSH key 大功告成。

    • 测试 SSH 连接,在 Git Bash 中敲入

    ssh -T git@github.com

  • 做完上面这些后 执行hexo deploy 大功告成

  • 但是我还是报错,hexo error fatal httprequestexception encountered….

    后来在https://github.com/hexojs/hexo/issues/3043 这里找到解决方法

    把repo的配置方式改一下,任何就可以了。

    1
    2
    3
    4
    deploy:
    type: git
    repo: git@github.com:anchen1204/anchen1204.github.io.git
    branch: master

    以上方法结合了这两篇文章:

    hexo:更换电脑,如何继续写博客

    多机更新 Hexo 博客

JavaScript

Object.assign()

语法:Object.assign(target, ...sources );

参数:

target:必需。可枚举属性复制到的对象。

…sources:必需。从其中复制可枚举属性的对象。

实例:

1
2
3
4
5
var first = { name: "Bob" };
var last = { lastName: "Smith" };

var person = Object.assign(first, last);
console.log(person);

看阮一峰大神的ES6-编程风格 :

对象尽量静态化,一旦定义,就不得随意添加新的属性。如果添加属性不可避免,要使用Object.assign方法。

1
2
3
4
5
6
7
8
9
10
11
// bad
const a = {};
a.x = 3;

// if reshape unavoidable
const a = {};
Object.assign(a, { x: 3 });

// good
const a = { x: null };
a.x = 3;

Promise

阅读(按左到右顺序): 大专栏  Day learn,day up/zh-CN/docs/Web/JavaScript/Guide/Using_promises" target="_blank" rel="noopener noreferrer">使用 Promises Promise

这两天又看了一遍Promise的内容,觉得这个对象真的非常好用。

做个总结呗,其实在项目中已经快乐地用起来了,很方便。

它存在的意义?还不是因为javaScript是世界上最好的…不对,说错台词了,应该是javaScript是单线程语言。

我们经常为了解决这个问题,使用的是回调函数,俗称callback,为了达到异步执行。比如setTimeout ,比如AJAX异步操作。

具体介绍不表,可以看文档。直接说使用吧。

1
new Promise( function(resolve, reject) {...} /* executor */  );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
new Promise((resolve, reject) => {
console.log('Initial');

resolve();
})
.then(() => {
throw new Error('Something failed');

console.log('Do this');
})
.catch(() => {
console.log('Do that');
})
.then(() => {
console.log('Do this whatever happened before');
});

.then()是链式调用,获取上一个Promise resolve返回的对象进行操作;

.catch() 原理同then,但是是针对Promise 的reject返回的对象进行操作;

注意:在一个失败操作(即一个 catch)之后可以继续使用链式操作,即使链式中的一个动作失败之后还能有助于新的动作继续完成。

即链式调用不会因为其中一个环节失败(reject)而中断。

好用组合:

Promise.resolve() &Promise.reject()

这两个是手动创建一个已经resolve或者reject的promise快捷方法。

Promise.race()Promise.all()

这两个用法一样,其实括号内都是多个Promise对象

但是不同的地方在于:

Promise.all(iterable) 方法返回一个 Promise 实例,此实例在 iterable参数内所有的 promise 都完成(resolved),或参数中不包含 promise 时回调完成(resolve);如果参数中 promise 有一个失败(rejected),此实例回调失败(reject),失败原因的是第一个失败 promise 的结果。

Promise.race(iterable)方法返回一个 promise,一旦迭代器中的某个promise解决或拒绝,返回的 promise就会解决或拒绝。

一句话,前者如果有一个失败,则之间返回第一个失败(catch里);后者只会返回第一个处理的失败或者成功。

举个实际可能会使用的例子:

Promise.all是很适合来解决多个Promise是用于获取请求基本信息,比如app里的登陆信息,ticket,版本号,它们都获取成功后可以接着执行可能下一步的请求。。。

Promise.race很适合容错现象,比如接口超时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//网络超时写法:Promise.race()
let promise1 = new Promise(resolve=>{
setTimeout(()=>{
return '数据已返回';
},5000)
})
let promise2= new Promise(reject=>{
setTimeout(()=>{
reject('网络超时')
},4000)
})
Promise.race([promise1,promise2]).then(res=>{
console.log(res)
}).catch(res=>{
console.log(res)
})
原文地址:https://www.cnblogs.com/lijianming180/p/12037931.html