手写防抖
定时器版本
functiion debounce(fn,wait){
let timer = null;
return function(){
let context = this,
args = [...arguments];
if(timer){
clearTimeout(timer);
timer = null;
}
timer = setTimeout(()=>{
fn.apply(context,args);
},wait)
}
}
手写节流
时间戳版本
function throttle(fn,delay){
let pertime = Date.now();
return function(){
let context = this,
args = [...arguments],
nowtime = Date.now();
if(nowtime - pertime > delay){
pertime = Date.now();
return fn.apply(context,args);
}
}
}
定时器版本
function throttle(fn,delay){
let timer = null;
return function(){
let context = this,
args = [...arguments];
if(!timer){
timer = setTimeout(()=>{
timer = null;
fn.apply(context,args);
},delay)
}
}
}
手写浅拷贝
function shallowCopy(object){
if(!object||typeof object !== "obeject"){
return;
}
let newObject = Array.isArray(object)?[]:{};
for(let key in object){
if(object.hasOwnProperty(key)){
newObject[key] = object[key];
}
}
return newobject;
}
手写深拷贝
function deepcopy(object){
if(!object||typeof object !== 'object'){
return;
}
let newobject = Array.isArray(object)?[]:{};
for(let key in object){
if(object.hasOwnProperty(key)){
newobject[key] = typeof object[key] === 'object' ? deepcopy(object[key]):object[key]
}
}
return newobject;
}
手写instanceof方法
function myInstanceof(left,right){
let proto = Object.getPropertyOf(left);
let prototype = right.prototype;
while(true){
if(!proto){ return false; }
if(proto == prototype){ return true; }
proto = proto.getPropertyOf(proto);
}
}
手写new方法
function objectFactory(){
let newobject = null,
result = null;
let constructor = Array.prototype.shift.call(arguments);
if(typeof constructor !== 'function'){
console.log('type error');
return;
}
newobject = Object.create(constructor.prototype);
result = constructor.apply(newobject,arguments);
let flag = result &&(typeof result ==='object'||typeof result ==='function');
return flag?result:newobject;
}
objectFactory(构造函数, 初始化参数);
手写call
function.prototype.myCall = function(context){
if(typeof this !== 'function'){
console.error("type error");
}
let args = [...arguments].slice(1);
let result = null;
context = context || window;
context.fn = this;
result = context.fn(...args);
delete context.fn;
return result;
}
手写apply
function.prototype.myApply = function(context){
if (typeof this !== "function") {
throw new TypeError("Error");
}
let result = null;
context = context || window;
context.fn = this.
if(arguments[1]){
result = context.fn(...arguments[1]);
}
else{
result = context.fn()'
}
delete context.fn;
return result;
}
手写bind
function.prototype.myBind = function(context){
if (typeof this !== "function") {
throw new TypeError("Error");
}
let fn = this;
let args = [...arguments].slice(1)
return function Fn(){
return fn.apply(
this instanceof Fn ? this:context,
args.concat(...arguments)
)
}
}
手写Object.create
function create(obj){
function fn(){};
fn.prototype = obj;
return new fn();
}
手写类型判断
function getType(value){
if(value===null){
return value+'';
}
if(typeof value ==="object"){
let valueClass=Object.prototype.toString.call(value);
let type=valueClass.split(" ")[1].split("");
type.pop();
return type.join("").toLowerCase();
}else{
return typeof value;
}
}
手写AJAX请求
const SERVER_URL = "/server";
let xhr = new XMLHttpRequest();
xhr.open("GET", SERVER_URL, true);
xhr.onreadystatechange = function() {
if (this.readyState !== 4) return;
if (this.status === 200) {
handle(this.response);
} else {
console.error(this.statusText);
}
};
xhr.onerror = function() {
console.error(this.statusText);
};
xhr.responseType = "json";
xhr.setRequestHeader("Accept", "application/json");
xhr.send(null);
手写Promise
手写Promise.then
手写Promise.all
手写Promise.race
使用Promise封装AJAX