console.log(typeof 1);
console.log(typeof '1');
console.log(typeof true);
console.log(typeof {});
console.log(typeof []);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof Symbol());
console.log(typeof BigInt(1));
判断值和类型是否相等时,使用===
判断值是否相等时,使用==
window.onload指的是文档和图片等资源全部加载完才执行
DOMContentLoaded指的是DOM加载解析完就执行
const oBody = document.getElementsByTagName('body')[0];
for (let i = 0; i < 10; i++) {
const oButton = document.createElement('button');
oButton.innerText = i;
oButton.onclick = function () {
alert(i);
}
oBody.appendChild(oButton);
}
for (var i = 0; i < 10; i++) {
((i) => {
const oButton = document.createElement('button');
oButton.innerText = i;
oButton.onclick = function () {
alert(i);
}
oBody.appendChild(oButton);
})(i);
}
function debounce (fn, wait = 1000) {
let timer;
return function (...args) {
clearTimeout(timer);
let result;
timer = setTimeout(() => {
result = fn.apply(this, args);
}, wait);
while (!result) {
continue;
}
return result;
}
}
function throttle (fn, wait = 1000) {
let previous = 0;
return function (...args) {
const now = +new Date();
if (now - previous > wait) {
fn.apply(this, args);
previous = now;
}
}
}
解决了异步无限回调地狱的问题,类似如下代码
ajax('/userlist', (data) => {
ajax('/user/' + data[0].id, (data) => {
ajax('/user/comments/' + data.commentId, (data) => {
ajax('/user/comment/' + data[0].id, (data) => {
})
})
})
})
ajax('/userlist').then((data) => {
return ajax('/user/' + data[0].id)
}).then((data) => {
return ajax('/user/comments/' + data.commentId)
}).then((data) => {
return ajax('/user/comment/' + data[0].id)
})