“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:
Let's assume a triangle is an object. Create a function : computearea(),
Follow the given Syntax and Code:
-
- 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 a JavaScript.
JavaScript supports seven built-in objects, which are
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.