- //防抖
- function debounce(func,wait=5000){
- let timer=0;
- return function(...args){
-
- if (timer) clearTimeout(timer)
- timer=setTimeout(()=>{
- console.log(2);
- func.apply(this,args)
- },wait)
- }
- }
-
- //节流
- function throttle(func,delay=2000){
- var timer=1;
- return function(...args){
- if(timer){
- timer=setTimeout(()=>{
- func.apply(this,args)
- timer=0
- },delay)
- }
- }
- }
- function _instanceof(example,classFunc){
- if (typeof example!=='object' || example==null) return false
- let proto=Object.getPrototypeOf(example);
-
- while(true){
- if (proto==classFunc.prototype) return true;
- proto=Object.getPrototypeOf(proto)
- }
-
- }
- console.log(_instanceof([], Array)); //true
- function myNew(fn, ...args){
- // 基于原型链 创建一个新对象
- let newObj=Object.create(fn.prototype);
- // 添加属性到新对象上 并获取obj函数的结果
- let res=fn.apply(newObj,args)
- // 如果执行结构有返回值并是一个对象,返回执行结果,否则,返回新创建的对象
- return typeof res==='object'?res:newObj;
- }
- // 用法
- function Person(name,age){
- this.name=name;
- this.age=age;
- }
- Person.prototype.say=function(){
- console.log(this.age);
- }
- let p1=myNew(Person,'poety',18);
- console.log(p1.name);
- console.log(p1);
- p1.say
- let obj = {
- val: [12, 4, 'pp'],
- name: 'name',
- age: 12,
- obj: {
- name: 'n',
- age: 3
- },
- }
- function deepClone(source) {
- let tarGetobj = source.constructor === Array ? [] : {};
- for (let keys in source) { //如果是数组循环索引 对象循环键名
- if (source[keys] && source[keys] instanceof Object) {
- tarGetobj[keys] = deepClone(source[keys])
- } else {
- tarGetobj[keys] = source[keys]
- }
- }
-
- return tarGetobj
- }
-
- let new obj = deepClone(obj);