Methods for Defining JavaScript Objects
- Using an Object Literal
- Using the
new
Keyword - Using an Object Constructor
- Using
Object.assign()
- Using
Object.create()
- Using
Object.fromEntries()
JavaScript Object Literal
An object literal is a list of property names:values inside curly braces {}.
{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Note:
An object literal is also called an object initializer.
Creating a JavaScript Object
Examples
Create an empty JavaScript object using {}
, and add 4 properties:
// Create an Object const person = {}; // Add Properties person.firstName = "John"; person.lastName = "Doe"; person.age = 50; person.eyeColor = "blue"; Create an empty JavaScript object using new Object(), and add 4 properties: // Create an Object const person = new Object(); // Add Properties person.firstName = "John"; person.lastName = "Doe"; person.age = 50; person.eyeColor = "blue";
Note:
The examples above do exactly the same.
But, there is no need to use new Object()
.
For readability, simplicity and execution speed, use the object literal method.
Object Constructor Functions
Sometimes we need to create many objects of the same type.
To create an object type we use an object constructor function.
It is considered good practice to name constructor functions with an upper-case first letter.
Object Type Person
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Note:
In the constructor function, this
has no value.
The value of this
will become the new object when a new object is created.
Now we can use new Person()
to create many new Person objects:
Example
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
const mySister = new Person("Anna", "Rally", 18, "green");
const mySelf = new Person("Johnny", "Rally", 22, "green");
Property Default Values
A value given to a property will be a default value for all objects created by the constructor:
Example
function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.nationality = "English"; }
JavaScript Object Methods
JavaScript Object Methods can be grouped into:
- General Methods
- Property Management Methods
- Object Protection Methods
General Methods
// Copies properties from a source object to a target object
Object.assign(target, source)
// Creates an object from an existing object
Object.create(object)
// Returns an array of the key/value pairs of an object
Object.entries(object)
// Creates an object from a list of keys/values
Object.fromEntries()
// Returns an array of the keys of an object
Object.keys(object)
// Returns an array of the property values of an object
Object.values(object)
// Groups object elements according to a function
Object.groupBy(object, callback)
Property Management Methods
// Adding or changing an object property
Object.defineProperty(object, property, descriptor)
// Adding or changing object properties
Object.defineProperties(object, descriptors)
// Accessing a Property
Object.getOwnPropertyDescriptor(object, property)
// Accessing Properties
Object.getOwnPropertyDescriptors(object)
// Returns all properties as an array
Object.getOwnPropertyNames(object)
// Accessing the prototype
Object.getPrototypeOf(object)
Object Protection Methods
// Prevents re-assignment
const car = {type:"Fiat", model:"500", color:"white"};
// Prevents adding object properties
Object.preventExtensions(object)
// Returns true if properties can be added to an object
Object.isExtensible(object)
// Prevents adding and deleting object properties
Object.seal(object)
// Returns true if object is sealed
Object.isSealed(object)
// Prevents any changes to an object
Object.freeze(object)
// Returns true if object is frozen
Object.isFrozen(object)
Using const
The most common way to protect an object from being changed is by using the const
keyword.
With const
you can not re-assign the object, but you can still change the value of a property, delete a property or create a new property.