Methods of an Object in JavaScript

Introduction

 
“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 the real world, If we take the bike as an object, which performs actions, such as blowing a 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 a triangle is an object. Create a function : computearea(),
 
Follow the given Syntax and Code:
  1. // method function  
  2. function computearea()  
  3. {  
  4.       var area=this.base*this.altitude*0.5  
  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:
  1. <SCRIPT type="text/JavaScript>  
  2. function triangle(b,a)  
  3. {  
  4.       this.base=b  
  5.       this.altitude=a  
  6. }  
  7. </SCRIPT>  
Now, as usual, we will call the method :
  1. <SCRIPT>  
  2. var mytriagle=new triangle (20,10)  
  3. alert("area="+mttriangle.area())  
  4. </SCRIPT> 
So this was the Syntax of Methods of a JavaScript.
 
JavaScript supports seven built-in objects, which are
 
Object2.jpg
 
These objects are also called core language objects.
 
All the above built-in objects have different objects. We will discuss this in the Next Post.
Foreantech
Foreantech - A complete online solution company.