Sekou Dosso
4 min readAug 23, 2020

--

JavaScript Objects

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. We will describe how to use objects, properties, functions, and methods, and how to create your own objects. In JavaScript, almost “everything” is an object.

  • Booleans can be objects (if defined with the new keyword)
  • Numbers can be objects (if defined with the new keyword)
  • Strings can be objects (if defined with the new keyword)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are always objects

All JavaScript values, except primitives, are objects.

JavaScript Primitives

A primitive value is a value that has no properties or methods. A primitive data type is data that has a primitive value. JavaScript defines 5 types of primitive data types:

  • string
  • number
  • boolean
  • null
  • undefined

Objects are Variables

JavaScript variables can contain single values: Objects are variables too. But objects can contain many values. The values are written as name : value pairs (name and value separated by a colon).

Object Methods

A method is a function associated with an object, or, simply put, a method is a property of an object that is a function. Methods are defined the way normal functions are defined, except that they have to be assigned as the property of an object.Methods are actions that can be performed on objects. Object properties can be both primitive values, other objects, and functions. An object method is an object property containing a function definition.

Creating a JavaScript Object

With JavaScript, you can define and create your own objects. There are different ways to create new objects:

  • Define and create a single object, using an object literal.
  • Define and create a single object, with the keyword new.
  • Define an object constructor, and then create objects of the constructed type.

Using an Object Literal

This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of name:value pairs (like age:50) inside curly braces {}.

The following example creates a new JavaScript object with four properties:
Spaces and line breaks are not important. An object definition can span multiple lines:

Using the JavaScript Keyword new

The following example also creates a new JavaScript object with four properties:

How to create Object method

objectName.methodname = functionName;

where objectName is an existing object, methodname is the name you are assigning to the method, and functionName is the name of the function.

You can then call the method in the context of the object as follows:

object.methodname(params);for example:let say we have an Object Car and a function:function Car(make, model, year, owner) {this.make = make;this.model = model;this.year = year;this.owner = owner;}function displayCar() {var result = `A Beautiful ${this.year} ${this.make} ${this.model}`;pretty_print(result);}We can add this function as Car property.function Car(make, model, year, owner) {this.make = make;this.model = model;this.year = year;this.owner = owner;this.displayCar = displayCar;}

Then you can call the displayCar method for each of the objects as follows:

car1.displayCar();

car2.displayCar();

The this Keyword In a function definition, this refers to the "owner" of the function.

JavaScript Objects are Mutable

Objects are mutable: They are addressed by reference, not by value. If person is an object, the following statement will not create a copy of person:

var x = person; // This will not create a copy of person.

The object x is not a copy of person. It is person. Both x and person are the same object. Any changes to x will also change person, because x and person are the same object.

Comparing objects

In JavaScript, objects are a reference type. Two distinct objects are never equal, even if they have the same properties. Only comparing the same object reference with itself yields true.

--

--