Javascript Object Method

A Javascript method is a piece of code that is called by name. Basically methods allow us to keep javascript code managable. Methods can accept zero or more parameter and allways return value when executed. A methods is executed when a call to that method is made anywhere within the script scope. Method can be constructed in variety of ways.

Using Constructor Method

In Javascript, their are different ways to defining method to an object.

Syntax1

function myObject() {
    this.method = function(){}
}
var o = new myObject();

Javascript Method Without Parametrs Example

function Person(name,address, email){
this.name = name;
this.address = address;
this.email = email;
this.display = function()
	{
		document.write(this.name);
		document.write(this.address);
		document.write(this.email);
	};
}
//Creating object
var obj = new Person("Jimi","12-13/A1","jimi@gmail.com");

//Invoke method
obj.display();

Javascript Method With Parametrs Example

function Person(){
this.setProperty = function(name,address,email)
	{
		this.name = name;
		this.address = address;
		this.email = email;
	};
}
this.display = function()
	{
		document.write(this.name);
		document.write(this.address);
		document.write(this.email);
	};
}
//Creating object
var obj = new Person();

//Passing parameters to method
obj.setProperty("Jimi","12-13/A1","jimi@gmail.com");

//Invoke method
obj.display();

Syntax2

function myObject() {}
myObject.prototype.method = function(){}
var obj = new myObject();

Javascript Method Without Parametrs Example

function Person(name,address, email){
this.name = name;
this.address = address;
this.email = email;
}

//Adding method using prototype
Person.prototype.display = function() {
    document.write("Name : " + this.name + " address : " 
	+ this.address + "Email : " + this.email);
};

//Creating object
var obj = new Person("Jimi","12-13/A1","jimi@gmail.com");

//Invoke method
obj.display();

Syntax3

var obj = {
    method: function(){}
}

Javascript Method Without Parametrs Example

var Person = function(name,address, email) {
    this.name = name;
	this.address = address;
	this.email = email;

    this.display = function() {
        document.write("Name : " + this.name + " address : " 
		+ this.address + "Email : " + this.email);
    };
};

//
//Creating object
var obj = new Person("Jimi","12-13/A1","jimi@gmail.com");

//Invoke method
obj.display();

Javascript Complete Method Example

<html>
<head>
<script type="text/javascript">
function Person(name,address, email){
this.name = name;
this.address = address;
this.email = email;
this.display = function()
	{
		document.write(this.name);
		document.write(this.address);
		document.write(this.email);
	};
}

//Adding method using prototype
Person.prototype.displayWithTitle = function() {
    document.write("Name : " + this.name + " Address : "  
	+ this.address + "Email : " + this.email);
};

//Creating object
var obj = new Person("Jimi","12-13/A1","jimi@gmail.com");

//Invoke method 
obj.display();

//Invoke prototype method
obj.displayWithTitle();

</script>
</head>
<body>
</body>
</html>