機(jī)械做網(wǎng)站好處百度推廣管家登錄
javascript 的對(duì)象包含許多有用的方法,可以幫助開(kāi)發(fā)人員輕松操作對(duì)象。讓我們通過(guò)簡(jiǎn)短的解釋和示例來(lái)了解一些最重要的內(nèi)容
- object.create()
- object.assign()
- object.keys()
- object.values()
- object.entries()
- object.freeze()
- object.seal()
- object.preventextensions()
- object.getprototypeof()
- object.setprototypeof()
- object.defineproperty()
- object.defineproperties()
- object.getownpropertydescriptor()
- object.getownpropertydescriptors()
- object.getownpropertynames()
- object.is()
- object.isfrozen()
- object.issealed()
- object.isextensible()
- object.fromentries()
- object.hasownproperty()
- object.hasown()
- object.groupby()(提議的功能,可能不完全可用)
object.create()
object.create() 是 javascript 中的一個(gè)方法,用于創(chuàng)建具有指定原型對(duì)象和可選屬性的新對(duì)象。與使用對(duì)象文字或構(gòu)造函數(shù)相比,它允許對(duì)對(duì)象的原型和屬性進(jìn)行更細(xì)粒度的控制。
const personprototype = {greet() {console.log(`hello, my name is ${this.name}`);}
};const john = object.create(personprototype);
john.name = "john";
john.greet(); // output: hello, my name is john
object.assign()
object.assign() 是一種內(nèi)置 javascript 方法,用于將所有可枚舉自身屬性的值從一個(gè)或多個(gè)源對(duì)象復(fù)制到目標(biāo)對(duì)象。它執(zhí)行淺復(fù)制并返回修改后的目標(biāo)對(duì)象。
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = object.assign(target, source);
console.log(result); // output: { a: 1, b: 2, c: 3 }
console.log(target); // output: { a: 1, b: 2, c: 3 } (target is also modified)
object.keys()
返回對(duì)象自己的可枚舉屬性名稱(chēng)(鍵)的數(shù)組
const obj = { a: 1, b: 2, c: 3 };
console.log(object.keys(obj)); // output: ['a', 'b', 'c']
object.values()
返回對(duì)象自身可枚舉屬性值的數(shù)組
?
const obj = { a: 1, b: 2, c: 3 };
console.log(object.values(obj)); // output: [1, 2, 3]
object.entries()
返回對(duì)象自身可枚舉屬性 [key, value] 對(duì)的數(shù)組
const obj = { a: 1, b: 2, c: 3 };
console.log(object.entries(obj)); // output: [['a', 1], ['b', 2], ['c', 3]]
object.freeze()
凍結(jié)對(duì)象,防止添加新屬性或更改或刪除現(xiàn)有屬性
const obj = { a: 1 };
object.freeze(obj);
obj.a = 2; // no effect, because the object is frozen
console.log(obj.a); // output: 1
object.seal()
密封對(duì)象,防止添加新屬性,但允許修改現(xiàn)有屬性。
const obj = { a: 1 };
object.seal(obj);
obj.a = 2; // allowed
delete obj.a; // not allowed
console.log(obj.a); // output: 2
object.preventextensions()
防止將任何新屬性添加到對(duì)象,但允許修改和刪除現(xiàn)有屬性
const obj = { a: 1 };
object.preventextensions(obj);
obj.b = 2; // not allowed
console.log(obj.b); // output: undefined
object.getprototypeof()
返回指定對(duì)象
的原型(即內(nèi)部[[prototype]])
const obj = {};
const proto = object.getprototypeof(obj);
console.log(proto); // output: {} (the default object prototype)
object.setprototypeof()
設(shè)置指定對(duì)象的原型。
const proto = { greet() { console.log('hello!'); } };
const obj = {};
object.setprototypeof(obj, proto);
obj.greet(); // output: 'hello!'
object.defineproperty()
在對(duì)象上定義一個(gè)新屬性或修改現(xiàn)有屬性,并使用屬性描述符的附加選項(xiàng)(例如,可寫(xiě)、可配置)。
const obj = {};
object.defineproperty(obj, 'a', {value: 42,writable: false, // cannot modify the value
});
obj.a = 100; // no effect because writable is false
console.log(obj.a); // output: 42
object.defineproperties()
使用屬性描述符在對(duì)象上定義多個(gè)屬性。
const obj = {};
object.defineproperties(obj, {a: { value: 42, writable: false },b: { value: 100, writable: true }
});
console.log(obj.a); // output: 42
console.log(obj.b); // output: 100
object.getownpropertydescriptor()
返回對(duì)象屬性的描述符。
const obj = { a: 1 };
const descriptor = object.getownpropertydescriptor(obj, 'a');
console.log(descriptor);
// output: { value: 1, writable: true, enumerable: true, configurable: true }
object.getownpropertydescriptors()
返回一個(gè)對(duì)象,其中包含對(duì)象自身屬性的所有屬性描述符
const obj = { a: 1 };
const descriptors = object.getownpropertydescriptors(obj);
console.log(descriptors);
// output: { a: { value: 1, writable: true, enumerable: true, configurable: true } }
object.getownpropertynames()
返回直接在對(duì)象上找到的所有屬性(包括不可枚舉的屬性)的數(shù)組。
const obj = { a: 1 };
object.defineproperty(obj, 'b', { value: 2, enumerable: false });
console.log(object.getownpropertynames(obj)); // output: ['a', 'b']
object.is()
比較兩個(gè)值是否相同(如 === 但處理 nan 等特殊情況)
console.log(object.is(nan, nan)); // output: true
console.log(object.is(+0, -0)); // output: false
object.isfrozen()
檢查對(duì)象是否被凍結(jié)
const obj = object.freeze({ a: 1 });
console.log(object.isfrozen(obj)); // output: true
object.issealed()
檢查物體是否被密封。
const obj = object.seal({ a: 1 });
console.log(object.issealed(obj)); // output: true
object.isextensible()
檢查是否可以將新屬性添加到對(duì)象。
const obj = { a: 1 };
object.preventextensions(obj);
console.log(object.isextensible(obj)); // output: false
object.fromentries()
將鍵值對(duì)數(shù)組轉(zhuǎn)換為對(duì)象
const entries = [['a', 1], ['b', 2]];
const obj = object.fromentries(entries);
console.log(obj); // output: { a: 1, b: 2 }
object.hasownproperty()
檢查對(duì)象是否擁有指定的屬性(不是繼承的)
const obj = { a: 1 };
console.log(obj.hasownproperty('a')); // output: true
object.hasown()
object.hasown() 是 es2022 中引入的較新方法,作為 object.hasownproperty() 的替代方法。它檢查一個(gè)對(duì)象是否具有帶有指定鍵的直接(自己)屬性,而無(wú)需查找原型鏈。
const obj = {name: 'alice',age: 25
};console.log(object.hasown(obj, 'name')); // true
console.log(object.hasown(obj, 'gender')); // false
object.groupby
object.groupby 是 ecmascript 2024 中為 javascript 提出的一個(gè)相對(duì)較新的功能,允許您根據(jù)通用標(biāo)準(zhǔn)對(duì)對(duì)象進(jìn)行分組。它尚未在所有環(huán)境中廣泛使用,因此在完全實(shí)現(xiàn)之前,它可能無(wú)法在許多瀏覽器或 javascript 引擎中工作。
const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 30 },{ name: 'Charlie', age: 25 },{ name: 'David', age: 30 },
];// Group objects by age
const groupedByAge = Object.groupBy(array, item => item.age);console.log(groupedByAge);/*
Expected Output:
{25: [{ name: 'Alice', age: 25 },{ name: 'Charlie', age: 25 }],30: [{ name: 'Bob', age: 30 },{ name: 'David', age: 30 }]
}
*/