Vue 源码 基础知识点

1、数据类型判断

const _toString = Object.prototype.toString

            function toRawType(value) {
                return _toString.call(value).slice(8, -1)
            }
console.log(toRawType({}))//Object

2、转换为字符串

function toString(val) {
                return val == null ?
                    '' :
                    typeof val === 'object' ?
                    JSON.stringify(val, null, 2) :
                    String(val)
            }

3、转换为number

function toNumber(val) {
                const n = parseFloat(val)
                return isNaN(n) ? val : n
            }

4、检测值是否已经定义

function makemapValue(str,expectsLowerCase) {
                const mapValue = Object.create(null)
                const list = str.split(',')
                for(let i = 0; i < list.length; i++) {
                    mapValue[list[i]] = true
                }
                return expectsLowerCase ?
                    val => mapValue[val.toLowerCase()] :
                    val => mapValue[val]
            }
            let isBuiltInTag = makemapValue('slot,component',true);
            console.log(isBuiltInTag('slot'))//true
            console.log(isBuiltInTag('abc'))//undefined
原文地址:https://www.cnblogs.com/mengfangui/p/9766798.html