WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Objects definition


Everything in JavaScript is an object: strings, numbers, arrays, functions ...

Also, JavaScript allows custom objects.


Everything is an object

JavaScript provides multiple built-in objects, such as String, Date, Array, and so on. Objects are just special data types with properties and methods.

  • Boolean can be an object.
  • A numeric can be an object.
  • String can also be an object
  • Date is an object
  • Mathematics and regular expressions are also objects
  • Array is an object
  • Even functions can be objects

JavaScript object

Objects are just a special kind of data. Object has attributes and methods .



Objects are Variables Containing Variables

JavaScript factors can contain single values:

Example

var individual = "John Doe";
Try it Yourself »

Objects are factors as well. Be that as it may, articles can contain numerous values.

The values are composed as name : value sets (name and esteem isolated by a colon).

Example

var individual = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Try it Yourself »


Object Properties

The named values, in JavaScript objects, are called properties.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue

Objects composed as name esteem sets are comparative to:

  • Associative clusters in PHP
  • Dictionaries in Python
  • Hash tables in C
  • Hash maps in Java
  • Hashes in Ruby and Perl

Object Methods

Methods are actions that can be performed on objects.

Object properties can be both crude qualities, different items, and functions.

An object method is an article property containing a function definition.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}

You will become familiar with techniques in the following chapters.


Creating a JavaScript Object

With JavaScript, you can define and create your own objects.

There are two different ways to create new objects:

  • Defining and creating instances of objects
  • Use functions to define objects and create new object instances

Using an Object Literal

This is the most straightforward approach to make a JavaScript Object.

Using an article exacting, you both characterize and make an item in one statement.

An object exacting is a rundown of name:value sets (like age:50) inside wavy supports {}.

The following model makes another JavaScript object with four properties:

Example

var individual = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Try it Yourself »

Spaces and line breaks are not essential. An article definition can traverse different lines:

Example

var individual = {
    firstName:"John",
    lastName:"Doe",
    age:50,
    eyeColor:"blue"
};
Try it Yourself »

Using the JavaScript Keyword new

The following model likewise makes another JavaScript object with four properties:

Example

var individual = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
Try it Yourself »


Using an Object Constructor

The precedents above are constrained much of the time. They just make a solitary object.

Sometimes we like to have a "object type" that can be utilized to make numerous objects of one type.

The standard approach to make a "object type" is to utilize an item constructor function:

Example

work person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
Try it yourself »

The above capacity (individual) is an article constructor.

Once you have an article constructor, you can make new objects of the equivalent type:

var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");

The this Keyword

In JavaScript, the thing called this, is the article that "owns" the JavaScript code.

The estimation of this, when utilized in a capacity, is the item that "owns" the function.

The estimation of this, when utilized in an article, is the item itself.

The this catchphrase in an article constructor does not have a esteem. It is just a substitute for the new object.

The estimation of this will turn into the new item when the constructor is utilized to make an object.

Built-in JavaScript Constructors

JavaScript has worked in constructors for local objects:

Example

var x1 = new Object();   /another Object object
var x2 = new String();   /another String object
var x3 = new Number();   /another Number object
var x4 = new Boolean()   /another Boolean object
var x5 = new Array();    /another Array object
var x6 = new RegExp();   /another RegExp object
var x7 = new Function(); /another Function object
var x8 = new Date();     /another Date object
Try it Yourself »

The Math() object isn't in the rundown. Math is a worldwide article. The new watchword can't be utilized on Math.

As you can see, JavaScript has object renditions of the crude information types String, Number, and Boolean.

There is no motivation to make complex articles. Crude qualities execute much faster.

And there is no motivation to utilize new Array(). Use cluster literals rather: []

And there is no motivation to utilize new RegExp(). Use design literals rather:/()/

And there is no motivation to utilize new Function(). Use work articulations: work () {}.

And there is no motivation to utilize new Object(). Use object literals rather: {}

Example

var x1 = {};           /new object
var x2 = "";           /new crude string
var x3 = 0;            /new crude number
var x4 = false;        /new crude boolean
var x5 = [];           /new array object
var x6 =/()/          /new regexp object
var x7 = function(){};  //new capacity object