解构赋值
给变量赋值的简洁写法
数组结构:const [变量] = [数组]
const arr = [1,2,3,[6,7,8]]
const [a,b,c,[,e,f]] = arr
对象解构:const {变量} = {对象}
const obj = {name:'xiaowang',age:20,{gender:'male',weight:200}}
const {name,age,{gender,weight}} = obj
构造函数
//使用new来进行构造函数
function Person (){
name:'yanhunag',
color:'yellow'
}
let xiaohuang = new Person('xiaohuang')
内置构造函数
//Object.keys 获取对象的所有键名
const obj = new Obeject()
Object.keys(obj)
//Object.values 获取对象的所有值
Object.values(obj)
//复制对象,合并
Object.assign(clone,obj,[select obj])
//如果添加后面的可选参数,则将所有对象进行合并,如果clone对象内部有后续对象的键名。则后续合并对象的键值会覆盖clone对象的键值
Array
//伪数组转换为真数组 Array.form(伪数组)
const o = {
0 : 'a',
1 : 'b',
2 : 'c',
length:3,
}
o.pop(); //Uncaught TypeError: o.pop is not a function
const arr = Array.from(o);
console.log(arr); //[]空数组,此时o未添加length
console.log(arr); //(3) ['a', 'b', 'c'] 此时o添加length,若length小于实际伪数组长度,则丢弃后续,多于则全为undefined (5) ['a', 'b', 'c', undefined, undefined]
String
const str = 'abcdef';
console.log(str.length); //6
console.log(str[2]); //'c'
//String.prototype.split('分隔符')
console.log(str.split('c')); //以c分割字符串,并不保留c
//(2) ['ab', 'def'] 可以使用''空字符串来分割每一个
//String.prototype.substring(indexStart,[indexEnd]) 截取一段字符串
const str = 'abcdef';
console.log(str.substring(3)); //def 从index(3)开始截取,如果后面没有添加种植截取索引值,则截取到最后
console.log(str.substring(1,4)); //bcd 从index(1)开始截取,截至到index(4),保留indexStart值,不保留indexEnd值
//String.prototype.includes('字符串',[index]) 检测字符串是否包含字符串的值
const str = 'abcdef';
console.log(str.includes('a')); //true
console.log(str.includes('f')); //true
console.log(str.includes('g')); //false
console.log(str.includes('a',3)); //false 从index值开始检测str是否含有'a'
//String.prototype.indexOf('字符串') 检测该字符串值在字符串中的索引值
const str = 'abcdef';
console.log(str.indexOf('a')); //1
console.log(str.indexOF('g')); //-1
//String.prototype.toUpperCase() 转换为大写
const str = 'abcdef';
consoel.log(str.toUpperCase()); // ABCDEF
//String.prototype.toLowerCase() 转换为小写
const str = 'ABCDEF';
console.log(str.toLowerCase()); //abcdef
//String.prototype.startsWith('字符串',[index]) 检测是否以字符串值开头
const str = 'abcdef';
console.log(str.startsWith('a')); //true
console.log(str.startsWith('b')); //false
console.log(str.startsWith('c',2)); //true
//String.prototype.endsWith('字符串',[index]) 检测字符串是否以字符串值结尾
const str = 'abcdef';
console.log(str.endsWith('b')); //false
console.log(str.endsWith('f')); //true
consoel.log(str.endsWuth('f',2)); //false
Number
//Number.prototype.toFixed(num) 保留.后num位,根据第三位选择四舍五入
const price = 1234.56789
console.log(price.toFixed(2)); //1234.57
const price = 1234.1234
console.log(price.toFixed(2)); //1234.12
const price = 1234.5678
console.log(price.toFixed()); //1235