回到目錄
概要
屬性的簡潔表示
略
屬性名表達(dá)式
例如 react 中 這樣只要一個(gè)方法就可以完成三個(gè) state 的修改
class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
onChange = (name, value) => {
this.setState({ [name]: value });
};
render() {
return (
<div>
<input
onChange={({ target: { value } }) => this.onChange("input1", value)}
/>
<input
onChange={({ target: { value } }) => this.onChange("input2", value)}
/>
<input
onChange={({ target: { value } }) => this.onChange("input3", value)}
/>
</div>
);
}
}
對象的描述對象
let obj = { foo: 123 };
Object.getOwnPropertyDescriptor(obj, "foo");
// {
// value: 123, // 值
// writable: true, // 是否可寫入
// enumerable: true, // 是否可枚舉
// configurable: true // 是否可以被刪除
// }
Object.getOwnPropertyDescriptors(obj);
// {
// a:{
// value: 123, // 值
// writable: true, // 是否可寫入
// enumerable: true, // 是否可枚舉
// configurable: true // 是否可以被刪除
// }
// }
super 關(guān)鍵字
this 和 super
| type |
target |
| this |
指向函數(shù)所在的當(dāng)前對象 |
| super |
指向當(dāng)前對象的原型對象。由于這個(gè)原因只能用在對象的方法中 |
const proto = {
foo: "hello"
};
const obj = {
foo: "world",
find() {
return super.foo;
}
};
Object.setPrototypeOf(obj, proto);
obj.find(); // "hello"
對象的擴(kuò)展符 (...) 與 對象的解構(gòu)
| 擴(kuò)展符 |
區(qū)別 |
| 數(shù)組 |
根據(jù)數(shù)組的遍歷器 Iterator 依次展開 |
| 對象 |
按照鍵名一一匹配 |
對象的擴(kuò)展符和解構(gòu) 跟 數(shù)組的用法基本一樣
特別的
let arr1 = [1, 2];
let arr2 = [2, 3];
let arr3 = [...arr1, ...arr2]; // [1, 2, 2, 3]
var obj1 = { a: 12, b: 13 };
var obj2 = { b: 10, c: 15 };
var obj3 = {
...obj1,
...obj2
}; // {a: 12, b: 10, c: 15}
對象的 api
| api |
作用 |
補(bǔ)充說明 |
| Object.is() |
用來比較兩個(gè)值是否嚴(yán)格相等,與嚴(yán)格比較運(yùn)算符(===)的行為基本一致。 |
Object.is() 補(bǔ)充 |
| Object.assign(target,obj1,obj2...) |
用于對象的合并,將源對象(source)的所有可枚舉屬性,復(fù)制到目標(biāo)對象(target) |
Object.assign() 補(bǔ)充 |
Object.is() 補(bǔ)充
+0 === -0; //true
NaN === NaN; // false
Object.is(+0, -0); // false
Object.is(NaN, NaN); // true
Object.assign() 補(bǔ)充
特點(diǎn)
| type |
說明 |
| 淺拷貝 |
Object.assign 方法實(shí)行的是淺拷貝,而不是深拷貝。也就是說,如果源對象某個(gè)屬性的值是對象,那么目標(biāo)對象拷貝得到的是這個(gè)對象的引用。 |
| 同名屬性的替換 |
嵌套的對象,一旦遇到同名屬性,Object.assign 的處理方法是替換,而不是添加。 |
| 數(shù)組的處理 |
可以用來處理數(shù)組,但是會(huì)把數(shù)組視為對象。即 按照鍵值對來展開,并執(zhí)行上面兩個(gè)特性 |
| 取值函數(shù)的處理 |
Object.assign 只能進(jìn)行值的復(fù)制,如果要復(fù)制的值是一個(gè)取值函數(shù) getter,那么將求值后再復(fù)制。(下方例子 getter) |
let a = {a:15};
let b = 12;
let c = 13
{...a,...b,...c} // {a:15}
// 等同于
let obj = {}
Object.assign(obj,a,b,c) // {a:15}
getter
const source = {
get foo() {
return 1;
}
};
const target = {};
Object.assign(target, source);
// { foo: 1 }
對象的其他方法
| api |
作用 |
補(bǔ)充說明 |
| Object.getOwnPropertyDescriptor(obj,name) |
返回某個(gè)對象指定屬性的描述對象 |
例子 1 |
| Object.getOwnPropertyDescriptors(obj) |
返回指定對象所有自身屬性(非繼承屬性)的描述對象。該方法的引入目的,主要是為了解決 Object.assign()無法正確拷貝 get 屬性和 set 屬性的問題。 |
例子 1 |
| Object.create(obj) |
以指定的對象為原型,生成一個(gè)新對象 |
例子 2 |
例子 1
const obj = {
foo: 123,
get bar() {
return "abc";
}
};
Object.getOwnPropertyDescriptor(obj, "bar");
// {
// get: [Function: get bar],
// set: undefined,
// enumerable: true,
// configurable: true }
// }
Object.getOwnPropertyDescriptors(obj);
// { foo:
// { value: 123,
// writable: true,
// enumerable: true,
// configurable: true },
// bar:
// { get: [Function: get bar],
// set: undefined,
// enumerable: true,
// configurable: true } }
例子 2
const obj1 = { a: 15 };
const obj2 = Object.create(obj1); // {}
obj2.a; // 15
obj2.__proto__; // {a: 15}
Object.getPrototypeOf(obj); // {a: 15}
跟原型操作相關(guān)的 api 或?qū)傩?/h3>
| api |
作用 |
說明 |
| __proto__ |
用來讀取或設(shè)置當(dāng)前對象的 prototype 對象。目前,所有瀏覽器(包括 IE11)都部署了這個(gè)屬性。 |
直接修改屬性就可以 (ES6 之后官方不推薦) |
| Object.setPrototypeOf(obj,proto) |
給 obj 對象指定原型對象為 proto |
官方推薦的方式 |
| Object.getPrototypeOf(obj) |
讀取 obj 對象的原型對象 |
官方推薦的方式 |
對象遍歷相關(guān)的 api
| api |
作用 |
返回 |
| Object.keys(obj) |
獲取所有可遍歷(enumerable)屬性的鍵名 |
鍵名組成的數(shù)組 |
| Object.values(obj) |
獲取所有可遍歷(enumerable)屬性的鍵值 |
鍵值組成的數(shù)組 |
| Object.entries(obj) |
獲取所有可遍歷(enumerable)屬性的鍵值對 |
鍵值對組成的數(shù)組 |
| Object.fromEntries(arr) |
Object.entries()的逆操作,根據(jù)鍵值對數(shù)組,生成一個(gè)對象 |
返回一個(gè)對象 |
看到這里了是不是感覺數(shù)組也有這幾個(gè) api?那我們再來看看他們在數(shù)組和對象上有什么區(qū)別
數(shù)組 和 對象 上這 keys、 values、 entries 的區(qū)別
| 項(xiàng)目 |
keys |
values |
entries |
| 數(shù)組上掛載對象 |
Array.prototype |
Array.prototype |
Array.prototype |
| 數(shù)組上使用方式 |
[1].keys() |
[1].values() |
[1].entries() |
| 數(shù)組上返回值 |
遍歷器(Iterator) |
遍歷器(Iterator) |
遍歷器(Iterator) |
| 對象上掛載對象 |
Object |
Object |
Object |
| 對象上使用方式 |
Object.keys(obj) |
Object.values(obj) |
Object.entries(obj) |
| 對象上返回值 |
數(shù)組 |
數(shù)組 |
數(shù)組 |
在實(shí)現(xiàn)對象的合并或者說復(fù)制的時(shí)候,我們有
更為詳細(xì)的對象知識
回到目錄