I always have difficulty grasping new concepts without seeing a real, basic, working example of what I am reading about. While I like the other explanation on stackoverflow, I'd really like to see a very basic example showing the difference between methods and functions in JavaScript that I can quickly run to learn more.
4 Answers
A method
is just a function that is a property
of an object. It's not a different type of object in javascript, but rather method
is just the descriptive name given to a function
that is defined as a property
of an object.
var myObj = {};
myObj.go = function() {alert("hi");}
myObj.go();
In this example, go
is a method on the myObj
object.
When a method is called as in the above example myObj.go()
, then the value of the this
pointer is set to the object that was involved in the invocation of the method (in this case myObj
).
Since global functions are also implicitly properties on the window
object, one could say that global functions are also methods on the window
object, but you do not need the window
designation in order to call them.
Local functions like inner()
in this function are just functions and not methods as they are not attached to a particular object:
function main() {
function inner() {
alert("hi");
}
inner();
}

- 683,504
- 96
- 985
- 979
This is a function and a function call:
function myFunction(){
alert("This is a function!");
}
myFunction();
This, on the other end, is a method call, because it is a member function of an object.
message.toUpperCase();
Here's the full code to create a class/methods and a call:
function Circle(x,y,r) {
this.xcoord = x;
this.ycoord = y;
this.radius = r;
}
Circle.prototype.retArea = function () {
return ( Math.PI * this.radius * this.radius );
};
var aCircle = new Circle(1,2,3);
var a = aCircle.retArea();

- 5,094
- 3
- 24
- 32
-
2Technically, not quite. In JavaScript, functions you declare "globally" are still properties of an object, namely, the global object. In common usage, you can say this, because no one ever really calls global functions for the purpose of accessing or mutating other properties on the global object. – Ray Toal Sep 23 '12 at 03:55
-
Point given. But I assumed OP was looking for examples for the common simple answer. ;-) (based on the link he provided) – mbinette Sep 23 '12 at 03:57
-
That's fair. Yes, for the usual case your answer is helpful. – Ray Toal Sep 23 '12 at 03:58
-
2How do I execute the example with the function Circle(). I thought it would have been just a case of adding an additional line alert(Circle.prototype.returnArea); but when I add this it outputs the entire function () code. – fakeguybrushthreepwood Sep 23 '12 at 04:36
-
You'd have to create a Circle object. See my edit (at the end of the post!)! :-) – mbinette Sep 23 '12 at 04:38
-
Sorry as a beginner in JavaScript, I'm still not sure what else I'd have to add on to this to get this example to work. – fakeguybrushthreepwood Sep 23 '12 at 04:44
-
Well, the example should work on it's own! But you need to display `a` to see something ;-) Try to add `alert( a );` at the end of the script :-) – mbinette Sep 23 '12 at 04:46
example:
function:
var f1 = function fBase() { ... }
function f2() { ... }
var f3 = function() { ... }
f1()
f2()
f3()
method:
var c = function oBase() {
this.method1 = function() { ... };
}
c.prototype.method2 = function() { ... }
var o = new c()
o.method1()
o.method2()
method json:
var o = { method1: function() { ... } }
o.method2 = function() { ... }
o.method1()
o.method2()

- 14,887
- 13
- 64
- 115
A function is a type which can be used to define a piece of code that can be executed by using call ("()") operator and may return data to the caller.
e.g.
define
function sayHello(){
return "Hello";
}
use
var result = sayHello();
Now, result will contian "Hello".
A method is a function that is defined inside an object and accessible through a property. For example, slice
is function defined over all string instances
e.g.
define
var obj = {
sayHello : function(){
return "Hello";
}
};
you can also define methods outside the object definition
var obj = {};
obj.sayHello = function(){
return "Hello";
};
use
var result = obj.sayHello();
We use methods in object oriented programming.
Refer:

- 2,059
- 12
- 13