拉勾大前端day01
文章目录
数组的解构:
const arr =[1,2,3]
const [,,three]=arr
console.log(three) //输出:3
const [first,...args]=arr
console.log(...args) //输出:[2,3]
const [first,second,three,fourth]=arr
console.log(fourth) //输出:underfind
如果想要给设定好的成员设置默认值:
const arr=[1,2,3]
const [one,two,three,four='aa']=arr
console.log(four)// 输出:aa
const path='a/b/c'
const [,two]=path.split('/')
console.log(two)// 输出:b
对象的解构
对象的解构不能像数组那样按照位置进行解构,因为对象没有固定的位置,只能按照属性名来进行解构。
const obj={name:'carson',age:22}
const {name}=obj
console.log(name)//输出:carson
const obj={name:'carson',age:22}
const name='cc'
const {name}=obj
console.log(name)//这样就会出现命名冲突,报错
如果存在命名冲突的时候,只能把解构的属性名进行重命名:
const obj={name:'carson',age:22}
const name='cc'
const {name:obj_name}=obj
console.log(obj_name)//输出:carson
同时还要设置默认值:
const obj={name:'carson',age:22}
const name='cc'
const {name:obj_name='jr'}=obj
console.log(obj_name)//输出:carson
模板字符串
普通的字符串只能一行输入,但模板字符串支持多行输入.
const str=`123,
234`
console.log(str)
//输出:123
// 234
模板字符串支持插值表达式
const name='Tom'
const str=`hey,${name}`
console.log(str)//输出:hey,Tom
带标签的模板字符串
const name='tom'
const gener='boy'
function tag(strings){
console.log(strings)//输出:['hey,','is a','.']
}
const str =tag`hey,${name}is a ${gener}.`
可见这个’标签‘是以插值表达式来进行分隔的.
const name='tom'
const gener='boy'
function tag(strings,name,sex){
return strings[0]+name+strings[1]+sex+strings[2]
}
const str =tag`hey,${name}is a ${gener}.
console.log(str)//输出:hey,tom is a boy.
字符串的拓展方法
inclubes
startsWith
endsWith
const str='Error:this is a error'
console.log(str.startsWith('Error'))//输出:true
函数参数默认值
参数设置默认值后,一定要放在所有参数的最后面。
funcation foo(one,two='true'){
console.log(two);//输出:true
}
foo()
剩余参数
剩余参数使用拓展运算符 ...args ,一定要放在所有参数的最后面,并且只能使用一次。
注意:...args和arguments不同,...args返回的是一个真数组,arguments返回是伪数组。
funcation foo(one,...args){
console.log(...args);//输出:[2,3,4]
}
foo(1,2,3,4)
箭头函数不会改变this的指向
this指向上下文函数this的指向,也就是指向最近的函数的this指向。
const person={
name:'tom',
sayHi:()=>{
console.log(this.name)
},
sayHello:function(){
setTimeout(()=>{
console.log(this.name)
},1000)
}
}
person.sayHi()//输出:underfind
person.sayHello()//输出:tom
对象字面量的增强
const name='tom'
class person={
name,//属性名和属性值一样,可以省略
a(){
console.log(this.name)//输出:tom
},//相当于 a:function(){...}
[1+1]:'22' //动态添加属性
}
Object.assign 复制对象
const target={
a:'1',
b:'2'
}
const source={
a;'11',
c:'33'
}
const result=Object.assign(target,source)
console.log(result)//输出:{a:'11',b:'2',c:'33'}
console.log(result===target)//输出:true
const obj={
name:'11'
}
function foo(obj){
const newObj=Object.assign({},obj)
newObj.name='22'
return newObj.name
}
console.log(foo(obj))//输出:22
console.log(obj.name)//输出:11
Object.is 判断数值相等
console.log(Object.is(NaN,NaN))//输出:true
console.log(NaN===NaN)//输出:false
console.log(Object.is(0,false))//输出:false
console.log(0===false)//输出:false
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论