Promise
一种异步处理的方法
现在应该不怎么用了,用的async awiat 多一点。但面试常考
先上例子,方便理解
const promise = new Promise(function(resolve,reject){
if(`success`){
resolve(value)
}else{
reject(error)
}
})
promise.then(function(value){
// success
},function(error){
// fail
});
同时需要注意的是Promise函数执行的顺序
let promise = new Promise(function(resolve, reject) {
console.log('Promise');
resolve();
});
promise.then(function() {
console.log('resolved.');
});
console.log('Hi!');
// Promise
// Hi!
// resolved
promise函数遇到则立即执行,而后等待同步代码执行完后,再执行回调函数
示例,说明函数执行的顺序以及规则
new Promise((resolve, reject) => {
resolve(1);
console.log(2);
}).then(r => {
console.log(r);
});
// 2
// 1
new Promise((resolve, reject) => {
return resolve(1);
console.log(2);
}).then(r => {
console.log(r);
});
// 1
达咩,学不动了,呜呜呜~~,有没有前端的小伙伴交流一下学习心得
数组相关
判断数组的方法
数组也是对象,其中数组拥有length属性,只是在正常的人为未干预的情况下,length等于最大索引+1. 很多数组的方法实际上是处理对象的方法
var arr = [1,2,3,1];
var arr2 = [{ abac : 1, abc : 2 }];
function isArrayFn(value){
if (typeof Array.isArray === "function") {
return Array.isArray(value);
}else{
return Object.prototype.toString.call(value) === "[object Array]";
}
}
alert(isArrayFn(arr));// true
alert(isArrayFn(arr2));// true
数组的相关知识 length 以及相关误区 不多BB 都在图里
var foo = [1,2,,,3]
console.dir(foo)
delete foo[4]
console.dir(foo)
foo[1] = undefined
console.dir(foo)
结果依次为
简单说一下console.log console.dir 其实没什么区别,正常都用log 想要更加清晰地表示JS对象属性就用dir