/

JavaScript Object: A Comprehensive Reference

JavaScript Object: A Comprehensive Reference

In this blog post, we will discuss the properties and methods of the JavaScript Object built-in object.

An object in JavaScript is any value that is not of a primitive type. Even arrays or functions are actually objects under the hood. There are multiple ways to create an object in JavaScript:

  1. Using object literal syntax:
1
2
const person = {}
typeof person //object
  1. Using the Object global function:
1
2
const person = Object()
typeof person //object
  1. Using the Object constructor:
1
2
const person = new Object()
typeof person //object
  1. Using Object.create():
1
const car = Object.create()

You can initialize the object with properties using any of the above syntaxes. All these ways are equivalent and give you access to the methods we will discuss below.

Another way to initialize an object is by using the new keyword before a function with a capital letter. This function serves as a constructor for the object, allowing you to initialize its properties using the arguments passed to the constructor function.

For example:

1
2
3
4
function Car(brand, model) {
this.brand = brand
this.model = model
}

You can create a new object using the constructor:

1
2
3
const myCar = new Car('Ford', 'Fiesta')
myCar.brand //'Ford'
myCar.model //'Fiesta'

Objects in JavaScript have properties, where each property has a name and a value. An object can be considered as a map or dictionary data structure.

The value of a property can be of any type, including another object. When a property value is a function, it is called a method.

Objects in JavaScript can inherit properties from other objects, which we will discuss in detail when we cover inheritance.

It’s important to note that objects in JavaScript are always passed by reference. If you assign a variable the same value of another, if it’s a primitive type like a number or a string, they are passed by value. However, if the value is an object, changes made to one reference will affect all other references to that object.

For example:

1
2
3
4
let age = 36
let myAge = age
myAge = 37
age //36
1
2
3
4
5
6
const car = {
color: 'blue'
}
const anotherCar = car
anotherCar.color = 'yellow'
car.color //'yellow'

Now, let’s take a look at the properties and methods of the Object object.

Built-in Object Properties:

  1. length: This property is always equal to 1.
  2. prototype: This property points to the Object prototype object, which is the object that all other objects inherit from.

Static Methods:

Static methods are called directly on the Object object and provide a convenient namespace for related functions.

  1. Object.assign(): ES2015 - Copies the values of all enumerable properties from one or more source objects to a target object.
  2. Object.create(): Creates a new object with the specified prototype object and properties.
  3. Object.defineProperties(): Defines new or modifies existing properties directly on an object, returning the object.
  4. Object.defineProperty(): Defines a new property directly on an object or modifies an existing property, returning the object.
  5. Object.entries(): ES2017 - Returns an array of a given object’s own enumerable property key-value pairs.
  6. Object.freeze(): Freezes an object, preventing new properties from being added, and existing properties from being modified or removed.
  7. Object.getOwnPropertyDescriptor(): Returns an object describing the configuration of a specific property on an object.
  8. Object.getOwnPropertyDescriptors(): Returns an object containing all own property descriptors for an object.
  9. Object.getOwnPropertyNames(): Returns an array of all properties (including non-enumerable properties) found directly in a given object.
  10. Object.getOwnPropertySymbols(): Returns an array of all symbol properties found directly in a given object.
  11. Object.getPrototypeOf(): Returns the prototype (i.e., the internal [[Prototype]] property) of the specified object.
  12. Object.is(): ES2015 - Determines whether two values are the same value.
  13. Object.isExtensible(): Determines if an object is extensible (i.e., if new properties can be added to it).
  14. Object.isFrozen(): Determines if an object is frozen.
  15. Object.isSealed(): Determines if an object is sealed.
  16. Object.keys(): Returns an array of a given object’s own enumerable property names.
  17. Object.preventExtensions(): Prevents any extensions of an object (i.e., prevents adding new properties).
  18. Object.seal(): Prevents adding or deleting properties, and marks all existing properties as non-configurable.
  19. Object.setPrototypeOf(): ES2015 - Sets the prototype (i.e., the internal [[Prototype]] property) for an object.
  20. Object.values(): Returns an array of a given object’s own enumerable property values.

Instance Methods:

Instance methods are called on an object instance and provide useful functionality directly on the object.

  1. hasOwnProperty(): Returns a boolean indicating whether the object has the specified property as a direct property.
  2. isPrototypeOf(): Checks if an object exists in another object’s prototype chain.
  3. propertyIsEnumerable(): Returns a boolean indicating whether the specified property is enumerable.
  4. toLocaleString(): Returns a string representing the object in a localized format.
  5. toString(): Returns a string representing the object.
  6. valueOf(): Returns the primitive value of the object.

Now that you have a comprehensive reference to the properties and methods of the JavaScript Object object, you can better utilize it in your JavaScript applications.

tags: [“JavaScript”, “Object”, “properties”, “methods”, “reference”]