Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Object Constructor Functions

Sometimes, we need to create multiple objects of the same type.

To define an object type, we use a constructor function.

It’s considered good practice to name constructor functions with an uppercase 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 initially.

 

However, when a new object is created, this refers to the newly created object.

Now, we can use new Person() to create multiple instances of the Person object.

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 assigned to a property will serve as the 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 categorized 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 modified is by using the const keyword.

With const, you cannot re-assign the object, but you can still modify its properties, delete a property, or add new ones.