对象解构例子

//函数参数解构 实际上就是对传入的对象参数进行解构

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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值