JavaScript Objects: Understanding the Basics

In JavaScript, the [
"Object"
]
class is the base class for all objects in the language. It is the parent of all objects, and provides a set of properties and methods that are available to all objects in the language.
It's important to note that in JavaScript all objects inherit from the [
"Object"
]
class, but it's a bit different from other OOP languages like Java, C#, etc. because of the prototype-based nature of the language.
JavaScript's objects are dynamic, meaning that you can add, modify, and delete properties and methods on objects at runtime. You can create a new object using the object literal notation [
"{}"
]
, using the [
"Object()"
]
constructor, or using the object.create() method.
It's a lot of power, but also a lot of responsibility. Since you have the ability to add new properties or methods to the object prototype chain, it can break or make code much more complex to debug.
One important thing to note is that JavaScript has a prototype chain that allows objects to inherit properties and methods from their parent objects. Each object has a prototype property that refers to the object from which it inherits properties and methods. This prototype chain is used to implement inheritance in JavaScript.
Creating objects in JavaScript
There are several ways to create objects in JavaScript, each with its own advantages and disadvantages. Here are a few of the most common methods:
Object literals: This is the most basic and common way to create an object in JavaScript. You can create an object using the object literal notation [
"{}"
]
, like this: [
"let myObject = {};"
]
. This method is simple, easy to read, and requires minimal code. The main disadvantage of this method is that it creates a new object every time the code is executed, so it can be less efficient if you are creating numerous objects.
Object constructor: The [
"Object()"
]
constructor can be used to create an object, like this: [
"let myObject = new Object();"
]
. This method is similar to the object literal notation, but it can be used to create an object with a specific prototype. One disadvantage is that it can make the code harder to read and understand.
[
"Object.create()"
]
: The [
"Object.create()"
]
method can be used to create an object with a specific prototype, like this: [
"let myObject = Object.create(prototypeObject);"
]
. This method is useful for creating an object that inherits from an existing object, without modifying the existing object. But it's important to note that this method does not create a new object by running the constructor function, it simply creates an object with the prototype specified. This method is less common and can be harder to read and understand.
Also is possible to use
[ "Object.create(null)" ]
to create an object without any prototype, this is useful when you need an object that doesn't inherit any properties or methods from the[ "Object" ]
class.
Factory functions: This method uses a function to create and return an object. A factory function is a plain function, not a class, that returns a new object. Here's an example:
1function createPerson(name) { 2 return { 3 name: name, 4 sayHello: function () { 5 console.log('Hello, my name is ' + this.name); 6 } 7 }; 8} 9let myObject = createPerson('John');
This method allows encapsulating the object creation logic, and it is easy to test and reuse.
The class syntax: In ES6 a new class syntax was introduced, this allows to create class constructors and instances.
1class Person { 2 constructor(name) { 3 this.name = name; 4 } 5 6 sayHello() { 7 console.log('Hello, my name is ' + this.name); 8 } 9} 10let myObject = new Person('John');
It's more verbose and requires more code but the main advantage is that it can make the code easier to understand and debug, and it's more similar to other OOP languages.
Each of these methods has its own advantages and disadvantages, depending on the specific use case. Some are more efficient, others are easier to read and understand, and some are better suited for certain types of applications.
Some of the most commonly used properties and methods of the [
"Object"
]
class
-
[ "Object.seal(obj)" ]
: This method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. -
[ "Object.freeze(obj)" ]
: This method is used to prevent new properties from being added to an object, prevent existing properties from being removed, and prevent existing properties, or their enumerability, configurability, or writability, from being changed. -
[ "Object.preventExtensions(obj)" ]
: This method prevents new properties from being added to an object. -
[ "Object.isSealed(obj)" ]
: This method returns a boolean indicating whether an object is sealed, which means that its properties cannot be added, removed, or modified. It can be useful for checking if an object is in a state where it should not be modified. -
[ "Object.isFrozen(obj)" ]
: This method returns a boolean indicating whether an object is frozen, which means that its properties cannot be modified and are read-only. It can be useful for ensuring that an object's properties remain constant and cannot be changed. -
[ "Object.isExtensible(obj)" ]
: This method returns a Boolean indicating whether the object is extensible (i.e., whether new properties can be added to it). -
[ "Object.keys(obj)" ]
: returns an array of the enumerable own properties of an object. -
[ "Object.values(obj)" ]
: This method returns an array of the object's own enumerable property values. -
[ "Object.entries(obj)" ]
: This method returns an array of the object's own enumerable property key-value pairs. -
[ "Object.fromEntries(iterable)" ]
: The Object.fromEntries() static method transforms a list of key-value pairs into an object -
[ "Object.is(value1, value2)" ]
: This method compares the values of two objects and returns a boolean indicating whether they are the same value. -
[ "Object.hasOwnProperty(propertyName)" ]
: This method returns a Boolean value indicating whether an object has a property with the specified name. -
[ "Object.defineProperty(obj, prop, descriptor)" ]
: and[ "Object.defineProperties(obj, props)" ]
: Those methods are used to define new properties on an object or modify existing properties. -
[ "Object.isPrototypeOf(obj)" ]
: This method returns a Boolean value indicating whether an object is a prototype for another object. -
[ "Object.assign(target, ...sources)" ]
: This method copies the values of all own enumerable properties from one or more source objects to a target object, and returns the target object. -
[ "Object.create()" ]
: This method creates a new object with the specified prototype object and properties.