1. 常见非指纹built-in函数(必须记住)

Created
Sep 13, 2023 02:44 AM
Tags
JS基础
 

1. unescape

unescape() 函数可对通过 escape() 编码的字符串进行解码。
unescape("abcdefg") 'abcdefg' unescape("\xa2") '¢' unescape("Vistit%20W3school%21") 'Vistit W3school!'

2. Function

Function类似于变量声明
var fun = new Function("a","return a"); // new一个方法 函数传参是a 返回值返回的是这个a // 相等于 function fun(a){ return a }

3. eval

eval 作用是将字符产当作JS的代码执行
eval("alert(111)") eval('debugger;alert(111)')

4. Array

制造一个数组 本身是个对象函数
a = new Array

5. Object

对某个值进行实例化
Object(1111) // Number {1111} === new Number(1111) Object("1111") // String {'1111'} Object(false) // Boolean {false}

5. Date

Date是一个构造函数 也是一个有返回值的函数
Date()
返回的是本地时间
a = new Date() a.getDate() // 1 号 a.getDay() // 3 周三

6. RegExp

正则 需要new 一个规则
a = new RegExp("n?") a.exec("asdasdksadnsakldjnsakdj")

7. indexOf

arry = [ "a", "b", "c", "d", ] arry.indexOf("a") // 0 arry.indexOf("b") // 1

8. hasOwnProperty

检测一个属性是否是对象的自有属性
function F(){ this.name = "自有属性" } F.prototype.name = "继承属性" var f = new F() f f.hasOwnProperty("name")

9. decodeURIComponent encodeURI encodeURIComponent

URL编码与解码
encodeURI("干嘛") '%E5%B9%B2%E5%98%9B' decodeURI('%E5%B9%B2%E5%98%9B') '干嘛'

10. Math .round 、random, parselnt 等强制转换

  • Math: 数字函数
  • round:Math.rand(0.5) == 1 四舍五入
  • random:Math.random() 随机数生成
  • parseInt:parseInt(15.55)

11. shift、pop、push、unshift

shift: 数组移位
pop: 数组删除末尾元素
push: 数组末尾添加
unshift: 数组前部添加

12. slice、 splice、split、 substring、 substr、 concat

slice : 拆分数组 不基于指针 || 字符串 也可以使用
var array = [1,2,3] array.slice(0,1) // [1] array.slice(0,2) // (2) [1, 2]
splice:拆分数组 基于指针 影响原数组 || 字符串也可以使用
var array = [1,2,3] array.splice(0,1) // [1] array // (2) [2, 3]0: 21: 3length: 2[[Prototype]]: Array(0)
split: 把字符串切分成数组
"asdsajhdjsadksaj".split("") (16) ['a', 's', 'd', 's', 'a', 'j', 'h', 'd', 'j', 's', 'a', 'd', 'k', 's', 'a', 'j']
join:把数组合并成列表
['a', 's', 'd', 's', 'a', 'j', 'h', 'd', 'j', 's', 'a', 'd', 'k', 's', 'a', 'j'].join("") 'asdsajhdjsadksaj'
substring:可以理解为切片
"abcdefg".substring(0,1) 'a' "abcdefg".substring(0,2) 'ab' "abcdefg".substring(0,3) 'abc' "abcdefg".substring(3,0) 'abc'
substr: 但是他不能写反
"abcdefg".substr(0,1) 'a' "abcdefg".substr(0,2) 'ab' "abcdefg".substr(0,3) 'abc' "abcdefg".substr(3,0) ''
concat: 数组合并 两个数组合并
a = new Array(1,2,3) b = new Array(4,5,6) a.concat(b) // (6) [1, 2, 3, 4, 5, 6] a.concat(a,b) // (9) [1, 2, 3, 1, 2, 3, 4, 5, 6]

13. String.fromCharCode、 charCodeAt

String.fromCharCode(): 将 Unicode 编码转为一个字符:
var n = String.fromCharCode(65); // A
String.fromCharCode():返回在指定的位置的字符的 Unicode 编码。
"A".charCodeAt() 65 [101,118,97,108].map(item=>{ return String.fromCharCode(item) }) ['e', 'v', 'a', 'l']

14. atob 、btoa、Uint8Array、 ArrayBuffer、 Int32Array、 Int16Array

atob: 把base64解码
btoa: 编码成base64
btoa('123') 'MTIz' atob('MTIz') '123'
Uint8Array: 数组类型表示一个 8 位无符号整型数组,创建时内容被初始化为 0
let uint8 = new Uint8Array(2); uint8[0] = 42;
ArrayBuffer、 Int32Array、 Int16Array都是ES2017的语法 可以自己百度搜

15. setTimeout 、setlnterval、clearTimeout

setTimeout: 延迟多少秒执行一次
setTimeout(()=>{console.log("你还好吗")}, 3000 ) 你还好吗
notion image
setlnterval: 每隔多少秒执行一次
a = setInterval(()=>{console.log("你还好吗")},1000) 你还好吗
notion image
clearTimeout:清除定时器
clearTimeout(a)