家庭网络搭建网站,素材中国免费素材网官网,计算机考试网页制作怎么做,做解密类网站可行文章目录 1、encodeURI、decodeURI2、encodeURIComponent、decodeURIComponent3、parseInt4、parseFloat5、String6、Number7、Boolean8、isNaN、Number.isNaN()9、JSON10、toString Js内置了一些函数和变量#xff0c;全局都可以获取使用#xff08;本文归纳非构造函数作用的… 文章目录 1、encodeURI、decodeURI2、encodeURIComponent、decodeURIComponent3、parseInt4、parseFloat5、String6、Number7、Boolean8、isNaN、Number.isNaN()9、JSON10、toString Js内置了一些函数和变量全局都可以获取使用本文归纳非构造函数作用的函数 1、encodeURI、decodeURI URIUniform Resource Identifier)标记一个字符串url出现特殊字符需要编码 const url http://www.abc.com?key1value 1key2value2
// 编码
const uriEn encodeURI(url)
// http://www.abc.com?key1value%201key2value2
// 空格变成了%20
// 解码 还原
decodeURI(uriEn)
// url2、encodeURIComponent、decodeURIComponent encodeURIComponent可以看成是对encodeURI的补充处理补充编码更多字符串 “; / ? : $ , #”大部分特殊字符会被编码!()*-._~0-9a-zA-Z 不会被编码 const url http://www.abc.com?key1value 1key2value2
// 编码
const uriEn encodeURIComponent(url)
// http%3A%2F%2Fwww.abc.com%3Fkey1%3Dvalue%201%26key2%3Dvalue2%2B
// 解码 还原
decodeURIComponent(uriEn)
// url3、parseInt 对字符串处理从左到右读取字符串得到整数先匹配特殊字符头比如0x表示16进制度没有特殊字符头碰到非数字字符结束包括小数点parseInt(string, radix); // radix表示进制 // 特殊字符头 0x 表示 16进制
parseInt(0x10) // 16
// 一般字符 radix默认为10
parseInt(1.1a) // 1
parseInt(11a) // 11
parseInt( 11a) // 11
parseInt() // NaN// 2 radix 36, 默认为10或者根据特殊字符头推导
parseInt(0x10) // radix 16
// 2 radix or radix 36 返回NaN
parseInt(1, 1) // NaN
// radix (0 || undefined) 搜索string有没有特殊字符头没有则更改为10
parseInt(1, 0) // 1// case
[1, 2, 3,].map(parseInt)
// 相当于
parseInt(1, 0) // 1 radix被更改为10
parseInt(2, 1) // NaN radix小于2
parseInt(3, 2) // NaN sting数字不能大于radix4、parseFloat 匹配数字直到匹配第一个小数点之后只匹配数字类似于num.num碰到非数字和小数点直接结束除非是第一个小数点 // 没有匹配到数字都会返回NaN
parseFloat(a1.11) // NaN
parseFloat(1a.11) // 1
parseFloat(1.a11) // 1
parseFloat(1.1a1) // 1.15、String
// 字符串强转
String(true) // true
// 引用数据调用对应的toString
String([1,1]) // 1,1 // Array.toString
String({}) // [object Object] // Object.toString6、Number Number(num) // num undefined
Number(undefined) // NaN
// num true/false; 隐式转换
Number(true) // 1
Number(false) // 0
// num string 会进行数字推导纯数字会返回数字非数字返回NaN
Number(1.1) // 1.1
Number(true) // NaN
// 隐式转换
Number() // 0
Number([]) // 0 // [].toString()0
Number([1]) // 17、Boolean
// key 0、-0、null、false、NaN、undefined、
Boolean(key) false
// 其余情况
Boolean(key) true8、isNaN、Number.isNaN() isNaN(key) 遇到string类型先内部转数字再比较存在隐患Number.isNaN()只会匹配NaN不会转换 isNaN(1a) // true
Number.isNaN(1a) // false
Number.isNaN(NaN) // true9、JSON 注意JSON.stringify参数注意JSON.stringify有些字段具有以下值不会序列化undefinedfunction const obj {name: Jason,age: 18,color: red,null: null,undefined: undefined, // 未被序列化fun: function () {}, // 未被序列化
};
// JSON.stringify(string, replaceFun: (key, value) value, indent)
// replaceFun表示转换函数indent表示换行后的缩进需要把字符串展示在页面上可以使用
// value自增并返回新值、value自增但是返回旧值
JSON.parse(JSON.stringify(obj, (key, value) (key age ? value : value), 2));// {name: Jason, age: 19, color: red, null: null}10、toString
// 借用Objece.toString 得到数据类型
Object.prototype.toString.call() // [object Undefined]
Object.prototype.toString.call() // [object String]
Object.prototype.toString.call([]) // [object Array]
Object.prototype.toString.call({}) // [object Object]// Array.toString 调用split(,); 如果参数不是数组则会去原型链上找到Object.prototype.toString
// Array.prototype.__proto__ 》Object.prototype
Array.prototype.toString.call([1,2]) // 1,2