Open qw789 opened 7 years ago
类的方法中有this时最好带一个bind。否则把这个方法单独使用时实例会是运行环境。
二、setTimeout(this.method, time)这种形式中的this,即上文中提到的第一个this,是根据上下文来判断的,默认为全局作用域,但不一定总是处于全局下,具体问题具体分析。
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
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); });
taskID=1 start.
VM20439:5 taskID=2 start.
VM20439:5 taskID=3 start.
Promise {
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"]
如何在 Web 关闭页面时发送 Ajax 请求 navigator.sendBeacon
.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)