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.
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.
Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Sep 23, 2012 at 3:49 fakeguybrushthreepwoodfakeguybrushthreepwood 3,0838 gold badges42 silver badges54 bronze badges4 Answers
Reset to default 5A 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();
}
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();
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()
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:
- Functions at MDN
- Objects at MDN