JavaScript Object Functions, Methods and Properties
In JavaScript, objects are a fundamental part of the language, and they come with a variety of built-in functions and methods. Here’s a list of important object functions and methods that you can use to manipulate and work with objects in JavaScript:
1. Object Constructors and Static Methods
Object.create(proto)
: Creates a new object with the specified prototype object and properties.Object.keys(obj)
: Returns an array of a given object's own property names, in the same order as we get with a normal loop.Object.values(obj)
: Returns an array of a given object's own enumerable property values.Object.entries(obj)
: Returns an array of a given object's own enumerable string-keyed property[key, value]
pairs.Object.assign(target, ...sources)
: Copies the values of all enumerable own properties from one or more source objects to a target object.Object.freeze(obj)
: Freezes an object, preventing new properties from being added and marking all existing properties as read-only.Object.seal(obj)
: Seals an object, preventing new properties from being added, but allowing existing properties to be modified.Object.preventExtensions(obj)
: Prevents new properties from being added to an object.Object.getPrototypeOf(obj)
: Returns the prototype (i.e., the internal[[Prototype]]
property) of the specified object.Object.setPrototypeOf(obj, proto)
: Sets the prototype of an object to another object ornull
.
2. Object Instance Methods
obj.hasOwnProperty(prop)
: Returns a boolean indicating whether the object has the specified property as its own property.obj.isPrototypeOf(obj)
: Returns a boolean indicating whether the specified object is in the prototype chain of the object.obj.propertyIsEnumerable(prop)
: Returns a boolean indicating whether the specified property is enumerable.obj.toString()
: Returns a string representation of the object.obj.valueOf()
: Returns the primitive value of the specified object.
3. JSON Methods
JSON.stringify(value, replacer, space)
: Converts a JavaScript value to a JSON string.JSON.parse(text, reviver)
: Parses a JSON string, constructing the JavaScript value or object described by the string.
4. Object Literal Methods (in ES6 and later)
In addition to the static methods, objects can also use methods defined in object literals:
Summary
This list covers a range of important object functions and methods available in JavaScript. Understanding these methods is essential for effectively working with objects, manipulating properties, and maintaining code readability and performance.
If you need more specific information about any of these methods or additional functionalities, feel free to ask!