<body><div id="text"></div></body><script>var vnode ={tag:'ul',props:{class:'list',on:{// 传入点击事件click:(e)=>{
console.log(e)},},},text:'',children:[{tag:'li',props:{class:'item',},text:'',children:[{tag:undefined,props:{},text:'牛客网',children:[],},],},{tag:'li',props:{},text:'',children:[{tag:undefined,props:{},text:'nowcoder',children:[],},],},],}const_createElm=(vnode)=>{const{ tag, props, text, children }= vnode
let node = document.createElement(tag)for(let prop in props){if(prop ==='on'){let events = props[prop]for(let key in events){
node.addEventListener(key,(e)=>{
events[key](e)})}}else{
node.setAttribute(prop, props[prop])}}
node.innerHtml = text
children.forEach((element)=>{
node.appendChild(_createElm(element))})return node
}let text = document.querySelector('#text')
text.appendChild(_createElm(vnode))</script>
手写lodash_get方法
//手写lodash get 方法const object ={a:[{b:{c:3}}]}const path =['a','0','b','c']functionmyGet(obj, path, defaultValue){let res = obj
path.forEach((k)=>{let value = res[k]if(value){
res = value
}else{
res = defaultValue
}})return res
}
console.log(myGet(object, path,'NULL'))
手写call apply bind函数
Function.prototype.myCall=function(context,...args){
context = context ==='undefined'? window :Object(context)let res =nulllet fn =Symbol()
context[fn]=this
res = context[fn](...args)delete context[fn]return res
}Function.prototype.myApply=function(context, arguments){
context = context ==='undefined'? window :Object(context)let res =nulllet fn =Symbol()
context[fn]=this
res = context[fn](...arguments)delete context[fn]return res
}Function.prototype.myBind=function(){let arr =Array.prototype.slice.call(arguments)let self = arr.shift()let fn =thisreturnfunction(){returnfn.apply(self, arr)}}
constmySetTimeout=(fn, timeout, arguments)=>{let start =newDate()constloop=()=>{let now =newDate()let timer = window.requestAnimationFrame(loop)if(now - start >= timeout){fn.apply(this, arguments)
window.cancelAnimationFrame(timer)}}
window.requestAnimationFrame(loop)}functionshowName(){
console.log('Hello')}let timerID =mySetTimeout(showName,1000)//注:这种写法会造成主线程死锁,不建议// const mySetTimeout = (fn, timeout, arguments) => {// const start = new Date()// while (1) {// if (new Date() - start >= timeout) {// fn.apply(this, arguments)// break// }// }// }
数组扁平化
arr =[1,[2,[3,[4]]]]const_flatten1=function(arr){return arr
.toString().split(',').map((item)=>{Number(item)})}const_flatten2=function(arr){let res =[]for(let item of arr){if(Array.isArray(item)){
res = res.concat(_flatten2(item))}else{
res = res.concat(item)}}return res
}const_flatten3=function(arr){return arr.reduce((pre, cur)=>{return pre.concat(Array.isArray(cur)?_flatten3(cur): cur)},[])}
不产生新数组删除原数组中的重复数字
constremove=(arr)=>{for(let i =0; i < arr.length; i++){if(arr.indexOf(arr[i])!== i){
arr.splice(i,1)
i--}}return arr
}
<body><button id="but">login</button></body><script>functionSingleTon(name){this.name = name
}SingleTon.prototype.getName=function(){
console.log(this.name)}const getInstance =(function(){var instance
returnfunction(name){if(!instance){
instance =newSingleTon(name)}return instance
}})()const a =getInstance('a')const b =getInstance('b')
console.log(a === b)
document.getElementById('but').onclick=function(){let div =createSingle()}functioncreateDiv(){let div = document.createElement('div')
div.innerHTML ='LOGIN'
document.body.appendChild(div)return div
}functiongetSingle(fn){var result
returnfunction(){if(!result){
result =fn.apply(this, arguments)}return result
}}let createSingle =getSingle(createDiv)