当前位置:网站首页>Easy to forget method

Easy to forget method

2020-11-10 12:04:41 yang

A deep clone

*Object.prototype.toString() Method , Will return a shape like "[object XXX]" String
Everything is object ,.call Inheriting objects toString Method *

function typeOf(obj){

    const toString = Object.prototype.toString
    const map = {
        '[object Boolean]':'boolean',
        '[object Number]':'number',
        '[object String]':'string',
        '[object Function]':'function',
        '[object Array]':'array',
        '[object Date]':'date',
        '[object RegExp]':'regExp',
        '[object Undefined]':'undefined',
        '[object Null]':'null',
        '[object Object]':'object'
    }
    return map[toString.call(obj)]
}

//deepCopy
function deepCopy(data){
    const t = typeOf(data)
    let o;
    if(t==='array'){
        o = []
        for(let i = 0;i<data.length;i++){
            o.push(deepCopy(data[i]))
        }
    }else if(t === 'object'){
        o = {}
        for(let key in data){
         o[key] = deepCopy(data[key])
        }
    }else{
        return data
    }
    return o
}

vue Dynamic display of class name according to conditions in

class attribute ( Array syntax ) , If the right side of the object satisfies , There is this class name The class name is satisfied according to the condition on the right , There is

 <label :class="wrapClasses"></label>
 
 
  computed:{
        wrapClasses(){
  
            return [
                `${prefixCls}-wrapper`,
                {
                    [`${prefixCls}-group-item`]:this.group,
                    [`${prefixCls}-wrapper-checked`]:this.currentValue,
                    [`${prefixCls}-wrapper-disabled`]:this.disabled,
                    [`${prefixCls}-${this.size}`]:!!this.size
                }
            ]
        }
        }

版权声明
本文为[yang]所创,转载请带上原文链接,感谢