1. 对象简洁语法
正常对象的写法
let name = "wuwei";
let age = 18;
let obj = {
name:name,
age: age,
showA: function(){
return this.name
}
}
简洁语法
let name = "wuwei";
let age = 18;
let obj = {
name, // 等价于 name:name,
age,
showA(){
return this.name
}
}
// 属性的简写
let [name,age]=['aa',10] //解构方式name解构出来是'aa',age解构出来是10
let obj ={name, //属性名与变量名重名就直接写一个,等价于name:name,age:age
age}
console.log(obj);
//方法的简写
// obj ={
// run:function(){
// console.log(this);
// }
// }
// 等价于
obj ={
run(){ //既保留了以前this规则,同时又简化了function关键字的使用
console.log(this);
}
}
obj.run()
二.属性名表达式
JavaScript 定义对象的属性,有两种方法。
// 方法一
let obj ={['a' + 'bc']:123}
obj.foo = true;
// 方法二 表达式方式属性名
let obj={};
obj['a' + 'bc'] =123;
console.log(obj);
但是,如果使用字面量方式定义对象(使用大括号),在 ES5 中只能使用方法一(标识符)定义属性。
var obj = {
foo: true,
abc: 123
};
ES6 允许字面量定义对象时,用方法二(表达式)作为对象的属性名,即把表达式放在方括号内。
let propKey = 'foo';
let obj = {
[propKey]: true,
['a' + 'bc']: 123
};
var obj ={['a' + 'bc']:123}; //es6字面量内部的属性也可以使用表达式
表达式还可以用于定义方法名。
let obj = {
['h' + 'ello']() {
return 'hi';
},
};
obj.hello() // hi
注意,属性名表达式与简洁表示法,不能同时使用,会报错。
// 报错
const foo = 'bar';
const bar = 'abc';
const baz = { [foo] };
// 正确
const foo = 'bar';
const baz = { [foo]: 'abc'};
let aa='aa';
let obj ={
[aa]:[aa] // 这种不可以简写
}
console.log(obj);
3. 对象新增
3.1. Object.is() 用来比较两个值是否相等
ES5 比较两个值是否相等,只有两个运算符:相等运算符(==)和严格相等运算符(===)。它们都有缺点,前者会自动转换数据类型,后者的NaN不等于自身,以及+0等于-0。
JavaScript 缺乏一种运算,在所有环境中,只要两个值是一样的,它们就应该相等。
ES6 提出同值相等算法,用来解决这个问题。Object.is就是部署这个算法的新方法。它用来比较两个值是否严格相等,与严格比较运算符(===)的行为基本一致。
对于基本数据类型;我们用户肉眼上看着相等就为true,看着不等就为false
Object.is(NaN,NaN); // true
NaN === NaN; // false
Object.is(+0,-0); // false
+0 === -0 // true
Object.is({}, {})
// false
3.2. Object.assign() 用来合并对象的
let 新对象 = Object.assign(目标对象,需要合并的对象)
// 返回目标对象
//复制一份数组
let obj={name:'aa'};
let objNew = Object.assign({},obj) //复制一份obj内容给objNew
console.log(objNew);
console.log(obj===objNew);
// 也可以是数组的合并
let arr=[10,20,30];
let obj =Object.assign({},arr);
console.log(obj);
3.2.1 如果属性名有重复
如果合并的对象的属性名有重复就是后面的覆盖前面的
let json = {name:'wuwei'}
let json2 = {name:'haha',age:18}
let json3 = {name:'xiaxia',age:18,sex:1}
let obj = Object.assign({},json,json2,json3);
// {name: "xiaxia", age: 18, sex: 1}
3.2.2. 如果只有一个参数,Object.assign会直接返回该参数。
const obj = {a: 1};
Object.assign(obj) === obj // true
3.2.3 由于undefined和null无法转成对象,所以如果它们作为参数,就会报错。
Object.assign(undefined) // 报错
Object.assign(null) // 报错
3.2.4 可以利用这个方法来拷贝数组
let arr = [1,2,3]
let arr2 = Object.assign([],arr);
console.log(arr2); // [1, 2, 3]
arr2.push(4);
console.log(arr2); // [1, 2, 3, 4]
console.log(arr); // [1, 2, 3]
//例子
let result={
'姓名':"xiaoming",
'学校':'四小',
'爱好':'读书、游泳'
}
//后台给的对象,属性名是中文的,所以使用对象合并assign重整数据
let obj =Object.assign({},result,{ //尽量不要改变原数据
name:result['姓名'],
school:result['学校'],
like:result['爱好']
})
console.log(obj);
//也可采用扩展运算符重整数据
let result={
'姓名':"xiaoming",
'学校':'四小',
'爱好':'读书、游泳'
}
let obj ={
...result,
name:result['姓名'],
school:result['学校'],
like:result['爱好']
}
console.log(obj);
3.3. Object.keys() Object.entries() Object.values()
console.dir(Object); //为了让对象能用迭代器,js在Object构造函数身上添加了三个方法values() keys entries。
let arr = [10,20,30];
for(let val of arr.values()){ //而数组的方法在它的原型上arr.valuse()
console.log(val);
}
//对象没有迭代器,因此对象不能使用for of
let obj ={
name:"小米",
age:19
}
for(let val of Object.values(obj)){ //为了让对象能用迭代器,js在Object构造函数身上添加了三个方法values() keys entries。要传参,告诉是哪个对象
console.log(val);
}
for(let key of Object.keys(obj)){
console.log(key);
}
for(let item of Object.entries(obj)){
console.log(item); //属性名 属性值数组对
}
for(let [name,age] of Object.entries(obj)){ //解构的散值对
console.log(name,age);
}
console.log(Object.values(obj)); //返回的是 值 的数组,严格说不是迭代器,数组上有这三个方法
//以上更简单的方法
let obj ={
name:"小米",
age:19
}
console.dir(Object);
let {values,keys,entries} = Object; //解构
for(let val of values(obj)){
console.log(val);
}
for(let key of keys(obj)){
console.log(key);
}
for(let item of entries(obj)){
console.log(item);
}
for(let [name,age] of entries(obj)){
console.log(name,age);
}
循环json 和对象
ES2017新增的
let json = {
name: "wuwei",
age : 18,
sex: "男"
}
// 遍历属性
for(let key of Object.keys(json)){
console.log(key); // name age sex
}
// 遍历属性值
for(let val of Object.values(json)){
console.log(val); // wuwei 18 男
}
// 遍历键值对
for(let item of Object.entries(json)){
console.log(item); // ["name", "wuwei"] ["age", 18] ["sex", "男"]
}
// 解构遍历的键值对
for(let [key,value] of Object.entries(json)){
console.log(key,value); // name wuwei age 18 sex 男
}
也可以通过解构将Object身上的是哪个方法解构出来
// 解构Object身上的方法
let {keys,values,entries} = Object;
let json = {
name: "wuwei",
age : 18,
sex: "男"
}
// 遍历属性
for(let key of keys(json)){
console.log(key); // name age sex
}
// 遍历属性值
for(let val of values(json)){
console.log(val); // wuwei 18 男
}
// 遍历键值对
for(let item of entries(json)){
console.log(item); // ["name", "wuwei"] ["age", 18] ["sex", "男"]
}
// 解构遍历的键值对
for(let [key,value] of entries(json)){
console.log(key,value); // name wuwei age 18 sex 男
}
4.对象的扩展运算符
4.1 解构赋值与...运算符
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x // 1
y // 2
z // { a: 3, b: 4 }
由于解构赋值要求等号右边是一个对象,所以如果等号右边是undefined或null,就会报错,因为它们无法转为对象。
let { x, y, ...z } = null; // 运行时错误
let { x, y, ...z } = undefined; // 运行时错误
解构赋值必须是最后一个参数,否则会报错。
let { ...x, y, z } = obj; // 句法错误
let { x, ...y, ...z } = obj; // 句法错误
注意,解构赋值的拷贝是浅拷贝,即如果一个键的值是复合类型的值(数组、对象、函数)、那么解构赋值拷贝的是这个值的引用,而不是这个值的副本。
let obj = { a: { b: 1 } };
let { ...x } = obj;
obj.a.b = 2;
x.a.b // 2
扩展运算符(...)用于取出参数对象的所有可遍历属性,拷贝到当前对象之中。
let z = { a: 3, b: 4 };
let n = { ...z };
n // { a: 3, b: 4 }
这等同于使用Object.assign方法。
let aClone = { ...a };
// 等同于
let aClone = Object.assign({}, a);
网友评论