startsWith 是查询字符串开始
endsWith 是查询字符串结束
includes 是查询字符串是否包含
// 字符串扩展 startsWith / endsWith /includes
const message = "hi string message ."
console.log(message.startsWith('hi')); // true
console.log(message.endsWith('.')); // true
console.log(message.includes('string')); // true
console.log(message.includes('strings')); //false
参数默认值
// 参数默认值
function show(age=18) {
console.log(age);
}
show(20) //20
show() //18
剩余参数
// 剩余参数
function food() {
console.log(arguments);
}
food(1,2,3,4) //[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4 }
function foo( ...args) {
console.log(args);
}
foo(1,2,3,4) // [1, 2, 3, 4 ]
function fo(firs, ...args) {
console.log(args);
}
fo(1,2,3,4) //[ 2, 3, 4 ]
展开数组
// 展开数组
const arr =[1,2,3,4,5,6]
console.log(...arr);
普通函数对比箭头函数,箭头函数不会改变this的指向
// 普通函数
const person ={
name:"张三",
sayHi:function () {
console.log(`hi,${this.name}`);
}
}
//对比, 使用箭头函数
const persong ={
name:"张三",
sayHi: () =>{
console.log(`hi,${this.name}`);
}
}
person.sayHi() //hi,张三
persong.sayHi() //hi,undefined
Object.assign 合并对象,
// Object.assign 使用 ,
// obj2 覆盖 obj1, 但是 合并之后对象地址是指 obj1
const obj1={
name:"张三",
age:18
}
const obj2={
name:"李四",
sex:"男"
}
let reslut = Object.assign(obj1,obj2)
console.log(reslut); // { name: '李四', age: 18, sex: '男' }
console.log(reslut === obj1); // true
console.log(reslut === obj2); // false
Reflect 操作对象
// Reflect 操作对象
const obj = {
name:"xiao",
age:18
}
console.log(Reflect.ownKeys(obj)); // [ 'name','age' ] 查询对象
console.log(Reflect.has(obj,"name")); // true 查询是否存在
console.log(Reflect.deleteProperty(obj,'age')); // true 删除 age 属性
console.log(Reflect.ownKeys(obj)); // [ 'name' ] 查询对象