JS类型转换

7/18/2020 js

ToPrimitive:数据类型转化为原始值

function ToPrimitive(target, type) {
	// 将目标对象转换为指定类型原始值[[primitive]]
	if (target == [[primitive]]) return target

	if (type == String) {
		if (target.toString() == [[primitive]]) return target.toString()
		if (target.valueOf() == [[primitive]]) return target.valueOf()
	}

	if (type == Number) {
		if (target.valueOf() == [[primitive]]) return target.valueOf() // object.valueOf()通常返回自身
		if (target.toString() == [[primitive]]) return target.toString()
	} else {
		// type未传入时优先valueOf()
		if (target.valueOf() == [[primitive]]) return target.valueOf()
		if (target.toString() == [[primitive]]) return target.toString()
	}
}

toStringtoNumber:数据类型转化为String or Number

toString(target) == ToPrimitive(target, String)
toNumber(target) == ToPrimitive(target, Number)

# +操作

计算 val1 + val2:

if (typeof ToPrimitive(val1) == 'string' || typeof ToPrimitive(val2) == 'string') return toString(val1) + toString(val2)
else return toNumber(val1) + toNumber(val2)

# ==判断

判断 x == y

let typeX = typeof x,
	typeY = typeof y

// 类型相同
if (typeX === typeY) {
	if (x === null || x === undefined) return true
	if (typeX === 'String' || typeX === 'Boolean' || typeX === 'Object') return x === y
	if (typeX === 'Number') {
		if (isNaN(x) || isNaN(y)) return false // 众所周知NaN不等于任何,包括自身
		if ((x === +0 && y === -0) || (x === -0 && y === +0)) return true
		return x === y
	}
}

// 类型不同
// 1.众所周知 null == undefined
if ((x === null && y === undefined) || (x === undefined && null)) return true

// 2.二者有一个为number,一个为string
if (typeX === 'Number' && typeY === 'String') return x == toNumber(y)
if (typeX === 'String' && typeY === 'Number') return y == toNumber(x)

// 3.二者有一个为boolean
if (typeX === 'Boolean') toNumber(x) == y
if (typeY === 'Boolean') toNumber(y) == x

// 4.二者有一个为Object,另一个为number或string
if (typeX === 'Object') ToPrimitive(x) == y
if (typeY === 'Object') ToPrimitive(y) == x

# 参考

1.JS 类型转换上 (opens new window)

2.JS 类型转换下 (opens new window)

    要么重构,
    要么享受!
    红莲华
    x
    loading...