GET和POST的區別
前端性能優化
加載優化css
DNS預解析前端
<link rel="dns-prefetch" href="http://example.com">
預加載數組
<link rel="preload" href="http://example.com">
預渲染瀏覽器
<link rel="prerender" href="http://example.com">
單頁應用緩存
腳本安全
css優化性能優化
緩存
緩存方式bash
緩存策略服務器
存儲
特性 | cookie | localStorage | sessionStorage | indexDB |
---|---|---|---|---|
數據生命週期 | 通常由服務器生成,能夠設置過時時間 | 除非被清理,不然一直存在 | 頁面關閉就清理 | 除非被清理,不然一直存在 |
數據存儲大小 | 4K | 5M | 5M | 無限 |
與服務端通訊 | 每次都會攜帶在header中,對於請求性能影響 | 不參與 | 不參與 | 不參與 |
繼承
原型鏈繼承cookie
function Parent(name) {
this.name = name;
}
Parent.prototype.say = function() {
console.log(this.name);
}
function Child(name) {
this.name = name;
}
Child.prototype = new Parent('father');
Child.prototype.eat = function() {
console.log('eat!!!');
}
Child.prototype.constructor = Child;
var child = new Child('Son');
child.say(); // Son
child.eat(); // eat!!!
複製代碼
類式繼承
function Parent(name) {
this.name = name;
}
Parent.prototype.say = function() {
console.log(this.name);
}
function Child(name, parentName) {
Parent.call(this, parentName);
this.name = name;
}
Child.prototype.eat = function() {
console.log('eat!!!');
}
var child = new Child('Son');
child.say(); // Uncaught TypeError: child.say is not a function
child.eat(); // eat!!!
複製代碼
組合式繼承
function Parent(name) {
this.name = name;
}
Parent.prototype.say = function() {
console.log(this.name);
}
function Child(name, parentName) {
Parent.call(this, parentName);
this.name = name;
}
Child.prototype = new Parent('father');
Child.prototype.eat = function() {
console.log('eat!!!');
}
Child.prototype.constructor = Child;
var child = new Child('Son');
child.say(); // Son
child.eat(); // eat!!!
複製代碼
寄生組合式繼承
function Parent(name) {
this.name = name;
}
Parent.prototype.say = function() {
console.log(this.name);
}
function Child(name, parentName) {
Parent.call(this, parentName);
this.name = name;
}
function factory(proto) {
function F() {}
F.prototype = proto;
return new F();
}
Child.prototype = factory(Parent.prototype);
Child.prototype.eat = function() {
console.log('eat!!!');
}
Child.prototype.constructor = Child;
var child = new Child('Son');
child.say(); // Son
child.eat(); // eat!!!
複製代碼
函數實現
composeFunctions(fn1,fn2,fn3,fn4)
等價於fn4(fn3(fn2(fn1))
。function composeFunctions() {
var args = Array.prototype.slice.apply(arguments);
return function() {
if (args.length == 1) {
return args[0].apply(this, Array.prototype.slice.apply(arguments));
}
return composeFunctions(...args.slice(1))(args[0].apply(this, Array.prototype.slice.apply(arguments)));
}
}
複製代碼
節流和防抖
// 防抖,只執行最後一次
function debounce (fn, wait) {
var timer = null;
return () => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, Array.prototype.slice.call(arguments, 0)), wait);
}
},
// 節流,每隔一段時間執行一次
function throttle(fn, wait) {
var timer = null;
return () => {
if (!timer) {
timer = setTimeout(() => {
fn.apply(this, Array.prototype.slice.call(arguments, 0));
timer = null;
}, wait);
}
}
}
複製代碼