Day learn,day up

前言

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

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

工具

hexo迁移

由于个人笔记本电脑最近瓦特了,因此不得不把博客迁出来,在这里作个记录,方便下次查找。 java

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

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

    npm install -g hexogithub

  • 安装后进入hexo/目录npm

  • 安装模块编程

    1
    2
    3
    4
    npm install
    npm install hexo-deployer-git --save
    npm install hexo-generator-feed --save
    npm install hexo-generator-sitemap --save
  • 而后从新配置githubjson

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

    Authenticating with GitHub from Gitpromise

    • 在 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)
})