ES2015 字符扩展方法,展开数组... , 普通函数对比箭头函数,Object.assign

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' ]  查询对象

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值