qw789 / blogs

生活中的记录
2 stars 2 forks source link

es6/underscore #4

Open qw789 opened 7 years ago

qw789 commented 7 years ago

.each(array, iteratee) ES5.1 array.forEach(iteratee) .map(array, iteratee) ES5.1 array.map(iteratee) .reduce(array, iteratee, memo) array.reduce(iteratee, memo) .reduceRight(array, iteratee, memo) array.reduceRight(iteratee, memo) .every(array, predicate) array.every(predicate) .some(array, predicate) array.some(predicate) .find(array, predicate) array.find(predicate) .pluck(array, propertyName) ES2015 array.map(value => value[propertyName]) _.includes(array, element) ES2016 array.includes(element)

qw789 commented 6 years ago

类的方法中有this时最好带一个bind。否则把这个方法单独使用时实例会是运行环境。

qw789 commented 6 years ago

二、setTimeout(this.method, time)这种形式中的this,即上文中提到的第一个this,是根据上下文来判断的,默认为全局作用域,但不一定总是处于全局下,具体问题具体分析。

qw789 commented 6 years ago

const promise = new Promise((resolve, reject) => { resolve('success1') reject('error') resolve('success2') })

promise .then((res) => { console.log('then: ', res) }) .catch((err) => { console.log('catch: ', err) })

then: success1

qw789 commented 6 years ago

const getRandom = () => +(Math.random()*1000).toFixed(0);

const asyncTask = taskID => new Promise(resolve => { let timeout = getRandom(); console.log(taskID=${taskID} start.); setTimeout(function() { console.log(taskID=${taskID} finished in time=${timeout}.); resolve(taskID) }, timeout); });

Promise.all([asyncTask(1),asyncTask(2),asyncTask(3)]) .then(resultList => { console.log('results:',resultList); });

qw789 commented 6 years ago

taskID=1 start. VM20439:5 taskID=2 start. VM20439:5 taskID=3 start. Promise {} VM20439:7 taskID=3 finished in time=102. VM20439:7 taskID=2 finished in time=426. VM20439:7 taskID=1 finished in time=783. VM20439:14 results: (3) [1, 2, 3]

qw789 commented 6 years ago

var a = new Promise(function (resolve,reject) { console.log(1); setTimeout(function () { console.log(11); resolve('aaa'); },1000); });

var b = new Promise(function (resolve,reject) { console.log(2); setTimeout(function () { console.log(22);
resolve('bbb'); },300); });

var p = Promise.all([a,b]); p.then(function(val) { console.log(val); });

}); // 1 // 2 // 22 // 11 // ["aaa", "bbb"]

qw789 commented 5 years ago

如何在 Web 关闭页面时发送 Ajax 请求 navigator.sendBeacon