电话号码模糊处理
对电话号码进行模糊处理,要进行一些类型转换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function phone(mobile,len=3){
return String(mobile).slice(0,len*-1)+'*'.repeat(len)
}
console.log(phone(98765432101))
</script>
</body>
</html>
类型转换使用技巧
这是字符串和数字之间的相互转化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//字符串转成数值
const string = "99"
console.log(typeof string)
console.log(string*1 + 78)
console.log(Number(string)+88)
//数值转换成字符串
const number = 66
console.log(typeof number)
const str = number + ""
console.log(typeof str)
console.log(typeof String(number))
//字符串的数字直接转换成数字,数字忽略
const string2 = "99.78houdunren" //如果以字符串开头结果将会是NaN,这个的结果还是99
console.log(parseInt(string2))
</script>
</body>
</html>
字符串转化成数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const cms = 'hdms'
console.log(cms.split(""))
</script>
</body>
</html>
数组转换成字符串:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const array = ["hdcms","houdunren"]
console.log(array.join(""))
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let str = "houdunren"
console.log(typeof str)
console.log(str.substr(3))
let cms = new String("hdcms")
console.log(typeof cms)
console.log(cms.substr(3))
</script>
</body>
</html>
上面有隐式类型转换
Boolean隐式转换原理
用于判断真假的布尔类型吗,有点有趣
比较的时候都转换成数值类型(true=1,false=0)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const boolean = new Boolean(true)
console.log(typeof boolean)
console.log(boolean.valueOf)
</script>
</body>
</html>
看看实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array = [1,2,3]
console.log(Number(array))
console.log(array ==false)
console.log(Boolean([]))
if([])console.log("hihi")
console.log(Boolean({}))
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array = []
console.log(Number(array))
console.log(array ==false)
console.log(Boolean([]))
if([])console.log("hihi")
console.log(Boolean({}))
</script>
</body>
</html>
array隐式转换成数值之后是1,不等于false
但是要是空数组之后转换就为0了,也就等于false
然后就算是空引用也不为假(空对象也是,空数组也是)
显示转换Booean类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 针对数值
let number = 0
console.log(typeof(number))
number = !!number //第一种
console.log(typeof(number))
console.log(Boolean(number)) //第二种
// 针对字符串
let string = 'houdunren'
console.log(!!string)
console.log(Boolean(string))
//针对数组
let array = []
console.log(!![])
console.log(Boolean([]))
//针对对象
let object = {}
console.log(!!{})
console.log(Boolean(object))
let date = new Date()
console.log(!!date)
console.log(Boolean(date))
</script>
</body>
</html>
这是不同类型的数据显式转换成bool类型的方法
boolean实例操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
while (true) {
const year = prompt('我哪年出生的').trim()
if(!year){
continue
}
console.log(year=='2005'?"回答正确":"回答错误")
break
}
</script>
</body>
</html>
这是应用的实例
Number声明方式与基本函数
接下来看看数值类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let number = 99.556
console.log(Number.isInteger(number)) //判断是不是整数
</script>
</body>
</html>
这是一些方法
标签:console,log,--,number,JS,Boolean,let,Document,续章 From: https://blog.csdn.net/chestnut_orenge/article/details/145101635