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);
}
}
}
复制代码