Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript object


May 06, 2021 JavaScript


Table of contents


JavaScript object


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

In addition, JavaScript allows custom objects.


Everything is an object

JavaScript provides several 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 digital can be an object.
  • A string can also be an object
  • The date is an object
  • Mathematical and regular expressions are also objects
  • An array is an object
  • Even functions can be objects

JavaScript object

An object is just a special kind of data. The object owns the property and the method.


Access the properties of the object

The property is the value associated with the object.

The syntax for accessing object properties is:

objectName.propertyName

This example uses the length property of the String object to get the length of the string:

var message="Hello World!";
var x=message.length;

After the above code is executed, the value of x will be:

12


The method of accessing the object

Methods are actions that can be performed on an object.

You can call a method using the following syntax:

objectName.methodName()

This example uses the toUpperCase() method of the String object to convert text to capital:

var message="Hello world!";
var x=message.toUpperCase();

After the above code is executed, the value of x will be:

HELLO WORLD!


Create a JavaScript object

JavaScript enables you to define and create your own objects.

There are two different ways to create a new object:

  • Define and create an instance of the object
  • Use functions to define objects, and then create new object instances

Create a direct instance

This example creates a new instance of an object and adds four properties to it:

person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";

Try it out . . .

Alternative syntax (using object literals):

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};

Try it out . . .
Tips:

You can practice using javaScript programming in this site

JavaScript object action!


Use the object constructor

This example uses a function to construct an object:

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

Try it out . . .

In JavaScript, this usually points to the function itself that we are executing, or to the object to which the function belongs (runtime)


Create a JavaScript object instance

Once you have an object constructor, you can create a new object instance, like this:

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


Add properties to the JavaScript object

You can add new properties to an existing object by assigning a value to it:

Suppose personObj already exists - you can add these new properties to it: firstname, lastname, age, and eyecolor:

person.firstname="John";
person.lastname="Doe";
person.age=30;
person.eyecolor="blue";

x=person.firstname;

After the above code is executed, the value of x will be:

John


Add methods to JavaScript objects

A method is nothing more than a function attached to an object.

Methods for defining objects within constructor functions:

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;

this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}

The value of the changeName() function name is assigned to the lastname property of the person.

Now you can try:

myMother.changeName("Doe");

Try it out . . .

JavaScript class

JavaScript is an object-oriented language, but JavaScript does not use classes.

In JavaScript, no classes are created, nor are objects created through classes (as in other object-oriented languages).

JavaScript is prototype-based, not class-based.


JavaScript for... in loop

JavaScript for... The in statement loops through the properties of the object.

Grammar

for (variable in object)
{
code to be executed
}

Note: for... The block of code in the in loop is executed once for each property.

Instance

Loop through the properties of the object:

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
txt=txt + person[x];
}

Try it out . . .
Here's a complete and simple dictionary implementation. Note that fusion is used externally, and some of the properties of the dynamic addition properties of the Object object are used internally.

Please study it carefully and examine its use: Try it out

Related articles

Easy to learn JavaScript: An overview of JavaScript objects