js基础

94 阅读2分钟

一、数组Array

  1. 新建
    let a = new Array(3)
    let b = new Array(3, 4)
    let c = new Array([1, 2])
    let d = Array.of(1, 2, 3)
    let e = Array.of(3)
    ​
    const arr = [1, 2, 3, 2, 3]
    const set = new Set(arr)
    Array.from(set)
    ​
    const arr3 = Array.prototype.slice.call(set)
    const arr2 = [...set]
    ​
    // 通过数组方法创建
    const arr4 = arr.slice(0, 3)
    const arr5 = arr.concat(arr4)
    ​
  1. 删除
    const arr = [1, 2, 3, 4, 5]
    arr.splice(1, 1)
    arr.pop()
    arr.shift()
    arr.length = 3
  1. 插入
    const arr = [1, 2, 3, 4, 5]
    arr[0] = 5
    arr.splice(1, 1, 2)
    arr.unshift(-1)
    arr.push(6)
  1. 遍历
    const arr = [1, 2, 3, 4, 5]
    for(let i = 0; i< arr.length; i++) {
        console.log(arr[i])
    }
    arr.forEach(console.log)
    arr.map(console.log)
  1. 搜索
    const arr = [1, 2, 3, 4, 5]
    if(arr.indexOf(2) === -1) {
        console.log(`2 is not in !`)
    }

    arr.filter(v => v === 3)
    arr.some(v => v === 3)
    arr.every(v => v > 0)

二、对象object

  1. 初始化
    const obj = {}
    const obj1 = { name: 'lzg' }
    const obj2 = new Object()
    obj2.name = 'lzg2'
    // 使用new
    function Animal(name){
        this.name = name
    }
    const dog = new Animal('dog')
    ​
    class Botany{
        constructor(name) {
            this.name = name
        }
    }
    const tree = new Botany('tree')
    // 使用原型
    const obj3 = Object.create(Array.prototype)
  1. 遍历
    const obj = { name: 'lzg' }
    for(let key in obj) {
        console.log(key, obj[key])
    }
    const keys = Object.keys(obj)
    const values = Object.values(obj)
    const symbols = Object.getOwnPropertySymbols()
  1. 修改
    const obj = {name: 'lzg'}
    obj.name = 'lzg1'const obj1 = {sex: 2}
    const obj2 = Object.assign({}, obj, obj1 )
  1. 克隆
    const obj = {name: 'lzg'}
    const cloneObj = JSON.parse(JSON.stringify(obj))
    ​
    function clone() {
        // do something
    }
    function deepClone() {
        // do something
    }
  1. 删除
    let obj = { name: 'lzg' }
    // 删除属性
    delete obj.name
    // 释放
    obj = null

三、函数Function

  1. 创建
    function fun() {
        console.log('这是一个函数')
    }
    const fun1 = function() {
        console.log('这是一个匿名函数')
    }
    const fun2 = () => {
        console.log('这是一个箭头函数')
    }
    const func3 = new Function(`
        console.log('这是一个new出来的函数')
    `)
  1. 调用
    function fun() {
        console.log(this.name)
    }
    fun()
    // 改变this指向
    const person = {name: 'lzg'}
    fun.call(person)
    fun.apply(person)
  1. 属性
    function Fun() {
        const args = [...arguments]
        console.log(arguments)
    }
    // 可以当静态变量使用
    Fun.age = 11
    console.log('Fun.age = ', Fun.age)

四、日期Date

  1. 创建
    new Date('2021-12-11')  // 在ios中有Bug,需要使用'2021/12/11'
    new Date('2021#12#11')
    new Date('2021-12-13 12:00:59')
    new Date('2021#12#11#9:59:59')
    const date = new Date(3600* 24 *1000*365)
    const date1 = new Date(date)
​
  1. 属性&方法
    const date = new Date('2021-12-11')
    // 静态方法
    Date.now()
    Date.parse('2021-12-11')
    // 获取年、月、日、时、分、秒
    date.getDate()
    date.getMonth()
    date.getFullYear()
  1. 转换函数
    function dateFormat(value) {
      if (!value) return ''
      const result = moment(value).format(DATE_FORMAT)
      return result || ''
    }

五、字符串string

  1. 类数组
    const str = 'hello, kitty'  // typeof(str) === 'string'
    const str1 = new String('hello, kitty') // typeof(str) === 'object'
    for(let i = 0; i< str.length; i++) {
        console.log(str[i])
    }
    str[0] = 111  // 不会进行修改
    const arr = [...str]
    arr[0] = 111 // 可以进行修改// 与数组的转换
    const arr = str.split(',')
    const str1 = arr.join(',')
  1. 方法
    const str = 'hello, kitty'
    // 截取
    str.slice(0, 3) // 数组的方法
    str.substring(1, 3)
    str.substr(1, 3)
    ​
    // 匹配和替换
    const regex = /kitty/i;
    str.replace(regex, 'snoopy')

六、Math

// 取整
    Math.ceil(0.5)  
    Math.floor(0.5)
    Math.round(0.4)
    // 比较
    Math.max(1, 2)
    Math.min(2, 3)
    // 随机数
    Math.random()* 10 + 1  // 1-10的随机数
    // 公式
    Math.PI// 组合
    function random(min, max) {
      const num = Math.floor(Math.random() * (max - min + 1)) + min;
      return num;
    }
    ​
    random(1, 10);

七、Promise

Promise A + 规范

    new Promise((resolve, reject) => {
        console.log('hello, kitty')
        resolve(3)
    }).then((data)=> {
        console.log(`resolve = ${data}`)
    }, (err)=> {
        console.log(`reject = `, err)
    })
    ​
    // 使用静态方法
    Promise.resolve(111).then(console.log)
    Promise.reject(444).then(null, console.log)
    ​
    // async
    async function getUser(){
        try{
            throw new Error('my fault')
            const data =   await Promise.resolve(9999)
            return data
        }catch(err) {
          return Promise.resolve('333')
        }
    }
    const p = getUser()
    p.then(console.log, console.log)
    ​
    // 多个promise
    Promise.all([p, p1]).then(console.log, console.log)