WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Objects definition


JavaScript Objects

In JavaScript, nearly "everything" is an object.

  • Booleans can be items (or crude information treated as objects)
  • Numbers can be articles (or crude information treated as objects)
  • Strings can be items (or crude information treated as objects)
  • Dates are dependably objects
  • Maths are dependably objects
  • Regular articulations are dependably objects
  • Arrays are dependably objects
  • Functions are dependably objects
  • Objects are objects

In JavaScript, all qualities, aside from crude qualities, are objects.

Primitive qualities are: strings ("John Doe"), numbers (3.14), genuine, false, invalid, and undefined.  


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 characterize and make your very own objects.

There are diverse approaches to make new objects:

  • Define and make a solitary item, utilizing an article literal.
  • Define and make a solitary item, with the catchphrase new.
  • Define an item constructor, and afterward make objects of the built type.

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