Objects

Javascript Objects

JavaScript is an object oriented programming language. In the previous lessons, you learned how to use different types of variables to represent different kinds of data in JavaScript. These variable can have only one value at a time. But Javascript objects can contain multiple values, as well as functions for working with these values. So, these Objects enable you to group related data and the functions into a single entity

An object is a collection of properties and these properties can be either primitives or other objects, including functions. Unlike most other object oriented programming languages, which are based on classes and class instances, JavaScript is based on prototypal inheritance in which objects inherit from other objects. So, these objects make it much easier to manage in your programs

How to create and use objects in Javascript

In the above code, student is an Object and name and age are Object properties and getName() is the method in the Object.

Call Object function

The above code will alert - Jack.

Object constructor

Object constructor

An object constructor is normally a regular JavaScript function that creates an object wrapper for the given value.

How to create a New Instance from a Constructor

Literal notation in Javascript Object

Object.create() in Javascript Object

We can create new objects in Javascript using Object.create() method.

Accessing Object Properties in JavaScript

We can access Object Properties in JavaScript using either the dot(.) notation or the bracket[] notation.

Object contain another Object

Object contain another Object

An Object can contain any data, including other objects too. The following example shows how to a Javascript Object contain other Object and access values.