- 分支
- 单分支
let ps1 = true;
if(ps1){
console.log('这是个单分支');
}
- 双分支
let ps2 = false;
if(ps2 === true){
console.log('这是正确的')
}else{
console.log('这是双分支')
}
- 多分支
let age =12
if(age>=18 && age<=35){
console.log('你是个青年人,努力挣钱')
}if(age>35 && age<=80){
console.log('您是老年人,请注意身体')
}else{
console.log('你是未成年,好好学习')
}
- 多分支语法糖
let age1=45
switch (true) {
case age1 >=18 && age1 <= 35:
console.log('你是个青年人,努力挣钱')
break
case age1 <=80:
console.log('您是老年人,请注意身体')
break
default:
console.log('你是未成年,好好学习')
}
2.对象字面量简化
1.for-in
let user= {id:1,username:'张三',class:'三年级'}
for(let key in user){
console.log(user[key])
}
2.forEach
let user= ['张三','李四','王二麻子']
user.forEach(item => {
console.log(item)
});
3.for-of
let user= ['王五','赵六','三儿']
for(let value of user){
console.log(value)
}
4.for
let user= ['红','黄','蓝','lv']
for(let i=0 ;i<user.length;i++){
console.log(user[i])
}
克隆
let item ={name:'张三',age:30,sex:'男'}
let { ...obj } =item
console.log(obj)
console.log(item)
console.log(item === obj)//不全等,表示不是同一个内容
传参
let item = {name:'张三',age:30,sex:'男'}
let show = function(item){
return `${item.name}:${item.age}:${item.sex}`
}
console.log(show({name:'法外狂徒',age:45, sex:'女'}))
对象结构传参
show = ({name,age,sex})=>`${name}:${age}:${sex}`
console.log(show({name:'王五',age:54,sex:'人妖'}))