//函数参数解构 实际上就是对传入的对象参数进行解构
function userId({id}) { //let {id}=user4
return id;
}
function getInfo({name, info:{ city, job:newJob, money=18000}}){
console.log(name,city,newJob,money);
}
var user4 = {
id: 10001,
name: "庄玉玉",
info: {
city: '杭州',
job: '部门经理',
},
};
console.log("userId: " + userId(user4));
getInfo(user4);
//解构函数对象返回值
function getStudentInfo() {
return {
name: '庄玉玉',
age: 18,
scores: {
math: 19,
english: 85,
chinese: 100
}
};
}
const { name:cname, scores: {math, english, chinese} } = getStudentInfo();
console.log(cname, math, english, chinese);
//动态参数解构
let student = getStudentInfo()
const getStudentInfo2 = function(key){ //key=>
const {[key]: value} = student; //[key]为动态参数变量写法
return value;
//const {age:value}= student
//return value
}
const getStudentInfo3 = function(pkey,key){ //key=>
const {[pkey]:{[key]:value}} = student; //[key]为动态参数变量写法
return value;
//const {age:value}= student
//return value
}
console.log(getStudentInfo2('name'));
console.log(getStudentInfo2('age'));
console.log(getStudentInfo3("scores","math"));
//user.id user4[key] [key]