JS常用的对象方法

分类: 365bet在线登录 时间: 2025-08-14 11:29:33 作者: admin 阅读: 6413

JavaScript 提供了丰富的对象方法,用于操作对象及其属性。以下是一些常用的对象方法及其代码演示。

1. Object.create()

Object.create() 方法创建一个新对象,使用现有的对象来提供新创建的对象的 __proto__。

const person = {

isHuman: false,

printIntroduction: function() {

console.log(`My name is ${

this.name}. Am I human? ${

this.isHuman}`);

}

};

const me = Object.create(person);

me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"

me.isHuman = true; // Inherited properties can be overwritten

me.printIntroduction(); // Output: "My name is Matthew. Am I human? true"

2. Object.assign()

Object.assign() 方法用于将所有可枚举的自有属性从一个或多个源对象复制到目标对象。它将返回目标对象。

const target = {

a: 1, b: 2 };

const source = {

b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target); // Output: { a: 1, b: 4, c: 5 }

console.log(returnedTarget); // Output: { a: 1, b: 4, c: 5 }

3. Object.keys()

Object.keys() 方法返回一个由给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和正常循环中一致。

const object1 = {

a: 'somestring',

b: 42,

c: false

};

console.log(Object.keys(object1)); // Output: ["a", "b", "c"]

4. Object.values()

Object.values() 方法返回一个由给定对象的自身可枚举属性值组成的数组,数组中属性值的排列顺序和正常循环中一致。

const object1 = {

a: 'somestring',

b: 42,

c: false

};

console.log(Object.values(object1)); // Output: ["somestring", 42, false]

5. Object.entries()

Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组,其排列顺序和通过手动循环对象属性的顺序一致。

const object1 = {

a: 'somestring',

b: 42

};

console.log(Object.entries(object1)); // Output: [["a", "somestring"], ["b", 42]]

6. Object.freeze()

Object.freeze() 方法可以冻结一个对象。一个被冻结的对象再也不能被修改;冻结了一个对象则不能向这个对象添加新的属性,不能删除已有属性,不能修改该对象已有属性的可枚举性、可配置性、可写性,以及不能修改已有属性的值。这个方法返回被冻结的对象。

const obj = {

prop: 42

};

Object.freeze(obj);

obj.prop = 33; // Throws an error in strict mode

console.log(obj.prop); // Output: 42

7. Object.seal()

Object.seal() 方法封闭一个对象,阻止添加新属性并将所有现有属性标记为不可配置。现有属性的值仍然可以更改。

const object1 = {

property1: 42

};

Object.seal(object1);

object1.property1 = 33;

console.log(object1.property1); // Output: 33

delete object1.property1; // Cannot delete when sealed

console.log(object1.property1); // Output: 33

8. Object.getOwnPropertyDescriptor()

Object.getOwnPropertyDescriptor() 方法返回指定对象上一个自有属性对应的属性描述符(descriptor)。

const object1 = {

property1: 42

};

const descriptor = Object.getOwnPropertyDescriptor(object1, 'property1');

console.log(descriptor);

/*

Output:

{

value: 42,

writable: true,

enumerable: true,

configurable: true

}

*/

9. Object.getOwnPropertyDescriptors()

Object.getOwnPropertyDescriptors() 方法返回指定对象所有自身属性的描述符。

const object1 = {

property1: 42

};

const descriptors = Object.getOwnPropertyDescriptors(object1);

console.log(descriptors);

/*

Output:

{

property1: {

value: 42,

writable: true,

enumerable: true,

configurable: true

}

}

*/

10. Object.defineProperty()

Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性,并返回此对象。

const object1 = {

};

Object.defineProperty(object1, 'property1', {

value: 42,

writable: false

});

object1.property1 = 77; // Throws an error in strict mode

console.log(object1.property1); // Output: 42

11. Object.defineProperties()

Object.defineProperties() 方法直接在一个对象上定义新的属性或修改现有属性,并返回该对象。

const object1 = {

};

Object.defineProperties(object1, {

property1: {

value: 42,

writable: true

},

property2: {

value: 30,

writable: false

}

});

console.log(object1.property1); // Output: 42

console.log(object1.property2); // Output: 30

12. Object.getPrototypeOf()

Object.getPrototypeOf() 方法返回指定对象的原型(即内部 [[Prototype]] 属性的值)。

const prototype1 = {

};

const object1 = Object.create(prototype1);

console.log(Object.getPrototypeOf(object1) === prototype1); // Output: true

13. Object.setPrototypeOf()

Object.setPrototypeOf() 方法设置一个指定的对象的原型(即内部 [[Prototype]] 属性)。

const prototype1 = {

};

const object1 = {

};

Object.setPrototypeOf(object1, prototype1);

console.log(Object.getPrototypeOf(object1) === prototype1); // Output: true

通过这些对象方法,可以更高效地创建、操作和管理 JavaScript 对象。这些方法对于编写更具可读性和可维护性的代码非常有用。

相关文章

365bet在线登录

拾柒印刷要多久

平板安卓office365破解版

拉莫斯新闻,视频,图片,简介,战绩

平板安卓office365破解版

怎么更改手机位置