Learn JavaScript

JavaScript Methods

JavaScript Methods are nothing but some functions (with a little bit difference), which allows JavaScript Objects to perform some manipulations. The key difference between a function and an object is: the function is an standalone unit of set of statements. On other hand, a method is always associated with an object and the keyword this is always used to referred to it.

Methods are used to show the desired manipulations, performing within an object on any output device.

You probably have noticed that, we kept write() method quite frequently, in fact every single program in our examples. write() method of document object is used to write the content on the document.

How to Define Methods for an Object

JavaScript Methods are the properties of an object that posses a function definition. In order to understand how to define a method for an object, consider it’s standard syntax given below:

Standard Syntax to Define a Method

object_name = {
methodName: function()
{
Set of atatements;
}
};

Accessing a Method

JavaScript Methods can be accessed by calling the objectName and a key for that method.

Standard Syntax to Access a Method

objectName.methodName();

Example: JavaScript program to understand the creation and access of a method.

<html>
<body>
<p id="jom"></p>
<script>
// creating an object
var car = {
  carCompany: "AUDI",
  model: "R8",
  year: 2006,
  mn : function() {
    return this.carCompany + " " + this.model + " " + "Launch in" + " " + this.year;
  }
};
// displaying the content of the object
document.getElementById("jom").innerHTML = car.mn();
</script>
</body>
</html>

Output

AUDI R8 Launch in 2006


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML, loops for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.