let arr=[1,2,3]
let type=Object.prototype.toString.call(arr)
let type2=type.split(" ")[1].split("")
console.log(type)
console.log(type2)
type2.pop()
console.log(type2.join(""))
Function.prototype.myCall=function(obj){
if(typeof this !== 'function'){
throw Error('wrong type')
}
var obj=obj||window
let args=[...arguments].slice(1)
console.log(...args)
obj.p=this
let result=obj.p(...args)
delete obj.p
return result
}
function add(a,b,c){
console.log(a+b+c)
console.log(this.a)
}
add.myCall(null,1,2,3)
let newfun=add.bind({a:2},2)
console.log(newfun)
let newfun2=new newfun(2,3,4)
console.log(newfun2)
Function.prototype.myBind=function(obj){
if(typeof this !== 'function'){
throw Error("type error")
}
let args=[...arguments].slice(1)
let _this=this
return function fn(){
return _this.apply(this instanceof fn?this:obj,args.concat(...arguments))
}
}
let testfun=add.myBind({a:1},3)
console.log(testfun)
let testfun2=new testfun(4,5,6)
console.log(testfun2)
let date=new Date('2021-09-25')
console.log(typeof date)
function deepClone(obj){
if(!obj||typeof obj !=="object"){
return
}
if(obj instanceof Date){
return new Date(obj)
}
if(obj instanceof RegExp){
console.log(obj+'1')
return new RegExp(obj)
}
let newobj=Array.isArray()?[]:{}
for(let key in obj){
if(obj.hasOwnProperty(key)){
newobj[key]=typeof obj[key]!=="object"?obj[key]:deepClone(obj[key])
}
}
return newobj
}
let exp=/cat/g;
let reg=new RegExp(exp)
console.log(reg)
var obj={
name:'lii',
add:add,
hobby:[1,'si',{a:1}],
date:date,
exp:reg
}
let newoo=deepClone(obj)
let newoo2=deepClone(obj)
newoo.date=new Date('2020-09-25')
console.log(newoo)
console.log(newoo2)
console.log(obj)
console.log(obj.exp)
console