Methods of an Object in JavaScript

“A method is a set of one or more statements that are executed by referring the name of the method.”

OR

“A method can be defined as an action performed by an object.”

In real world, If we take bike as an object, which performs actions, such as blowing horn or accelerating speed. A complex application is divided into methods; as a result, code becomes more flexible and easy to maintain and debug.

Two simple steps are here for defining an Object:

Object1.jpg

Let's assume triangle is an object. Create a function : computearea(),

Follow the given Syntax and Code:

// method function
function computearea()
{
var area=this.base*this.altitude*0.5
return area()
}

In the above code the computearea() function calculates the area of a triangle. Now, associate the function computearea() to the object triangle,

See given code:

<SCRIPT type="text/JavaScript>
function triangle(b,a)
{
this.base=b
this.altitude=a
}
</SCRIPT>

Now as usual we will call the method :

<SCRIPT>
var mytriagle=new triangle (20,10)
alert("area="+mttriangle.area())
</SCRIPT>

So this was the Syntax of Methods of an JavaScript.

JavaScript supports seven built-in objects, which are

Object2.jpg

These objects are also called core language objects.

All above built-in objects have different objects. We will discuss in Next Post.

Ebook Download
View all
Learn
View all
Foreantech - A complete online solution company.