Javascript Object Methods

A 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();

Method Without Parametrs

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();

Method With Parametrs

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();

Method Without Parametrs

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(){}
}

Method Without Parametrs

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();

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>

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.