第九节 ES6中对象的扩展
对象的赋值
let name = 'liyang'; let skill = '前端'; //之前 let objOld = {name:name,skill:skill}; console.log(objOld); //现在 let nowObj = {name,skill}; console.log(nowObj); //结果是一样的 //控制台打印 //{name: "liyang", skill: "前端"} // {name: "liyang", skill: "前端"}
key值的构建对象
//key值的构建对象 //场景化,假如key值未知, let keyval = 'skill'; var objKey = { [keyval]:"web" } console.log("objKey是",objKey); //打印结果 {skill: "web"}
//自定义对象的方法
let globalFun = { add:function(a,b){ return a + b } } console.log(globalFun.add(1,2));
这一个算是es5的知识吧!~_~
is()的用法
//is() 判断两个对象是否相等(严格相等),接收两个对象,返回值boolean // === 同值相等 console.log(+0 === -0); console.log(NaN === NaN); //打印结果 true false console.log(Object.is(+0,-0)); console.log(Object.is(NaN,NaN)); //打印结果 false true(与上面的截然相反) //总结 is() 判断两个对象是否相等(严格相等),=== 同值相等
//对象合并assign
let a = {name1:"liyang"}; let b = {name2:"JeneryYang"}; let c = {name3:"web"}; let all = Object.assign(a,b,c); console.log(all); //打印结果 //{name1: "liyang", name2: "JeneryYang", name3: "web"}