一、数组Array
- 新建
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)
- 删除
const arr = [1, 2, 3, 4, 5]
arr.splice(1, 1)
arr.pop()
arr.shift()
arr.length = 3
- 插入
const arr = [1, 2, 3, 4, 5]
arr[0] = 5
arr.splice(1, 1, 2)
arr.unshift(-1)
arr.push(6)
- 遍历
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)
- 搜索
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
- 初始化
const obj = {}
const obj1 = { name: 'lzg' }
const obj2 = new Object()
obj2.name = 'lzg2'
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)
- 遍历
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()
- 修改
const obj = {name: 'lzg'}
obj.name = 'lzg1'
const obj1 = {sex: 2}
const obj2 = Object.assign({}, obj, obj1 )
- 克隆
const obj = {name: 'lzg'}
const cloneObj = JSON.parse(JSON.stringify(obj))
function clone() {
}
function deepClone() {
}
- 删除
let obj = { name: 'lzg' }
delete obj.name
obj = null
三、函数Function
- 创建
function fun() {
console.log('这是一个函数')
}
const fun1 = function() {
console.log('这是一个匿名函数')
}
const fun2 = () => {
console.log('这是一个箭头函数')
}
const func3 = new Function(`
console.log('这是一个new出来的函数')
`)
- 调用
function fun() {
console.log(this.name)
}
fun()
const person = {name: 'lzg'}
fun.call(person)
fun.apply(person)
- 属性
function Fun() {
const args = [...arguments]
console.log(arguments)
}
Fun.age = 11
console.log('Fun.age = ', Fun.age)
四、日期Date
- 创建
new Date('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)
- 属性&方法
const date = new Date('2021-12-11')
Date.now()
Date.parse('2021-12-11')
date.getDate()
date.getMonth()
date.getFullYear()
- 转换函数
function dateFormat(value) {
if (!value) return ''
const result = moment(value).format(DATE_FORMAT)
return result || ''
}
五、字符串string
- 类数组
const str = 'hello, kitty'
const str1 = new String('hello, kitty')
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(',')
- 方法
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
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 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.all([p, p1]).then(console.log, console.log)