JS基础
2021-03-26 04:27
标签:类型转换 onclick 数字 null ESS gen code 题解 执行 JS基础 标签:类型转换 onclick 数字 null ESS gen code 题解 执行 原文地址:https://www.cnblogs.com/sheep2/p/12640231.html个人总结
关于delete
break 和 continue
list: {
for (let i=0; i
list: {
console.log(1)
console.log(2)
console.log(3)
break list //输出1 2 3
console.log(4)
}
typeof
console.log( typeof {} ) //object
console.log( typeof [] ) //object
console.log( typeof null ) //object
{}
空对象let obj = null
console.log(typeof undefined) //undefined
console.log(null == undefined) //true
console.log(null === undefined) //false
js代码类型
类型转换
错误捕捉
let x = NaN
try {
if (isNaN(x)) throw "值为NaN"
}
catch(err) {
console.log(`错误是:${err}`)
}
finally {
console.log(‘执行完毕~~‘)
}
/*
错误是:值为NaN
执行完毕~~
*/
精度问题
const a = 1.335
console.log((1.335 * 100).toFixed() / 100) //1.34
约束验证DOM方法
let oInput = document.getElementById(‘input‘)
let oBtn = document.getElementById(‘btn‘)
btn.onclick = function () {
if (oInput.checkValidity() == false) {
console.log(oInput.validationMessage)
}
}
JSON 与 js对象互相转换
//创建一个json字符串
const text = `{
"names": [
{"name": "蕾姆", "age": 18, "gender": "female"},
{"name": "拉姆", "age": 18, "gender": "female"},
{"name": "emt", "age": 14, "gender": "female"}
]
}`
//将json字符串转化为js对象
const obj = JSON.parse(text)
console.log(obj.names[0].name) //蕾姆
const obj = {
name: "486",
age: 20,
gennder: "male"
}
const json = JSON.stringify(obj)
console.log(json) //{"name":"486","age":20,"gennder":"male"}