DEV Community

imsabir
imsabir

Posted on

Method vs Function in Javascript

In Javascript every function is an object (A).
An Object is collection of {key:value} pair.

As per MDN:

A method is a function which is a property of an object. There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or Static Methods which are tasks that are called directly on an object constructor.

Values can be

*-> Primative: * Primative implies to data type such as number, string,boolean or can be another pair of object.

-> Function: Since its property of an object, so function is called as Method .

So, does it means that every function is a method but every method is not a function ? No

-> If a function is within the lexical environment / environment of an object, then that function is termed as method, we can invoke that by OurObject.OurMethod() (B)

var OurObject= {
name : "John snow",
OurMethod: function someFun(paramA, paramB) {
    // some code..
}
Enter fullscreen mode Exit fullscreen mode

-> A pure function is, a function which have no object associated with it.

function func(param1, ...args){
 // some code...
}
Enter fullscreen mode Exit fullscreen mode

As per Point A, we are saying every function is an object in Javascript, so, a function within within a function will be termed as method of that function.

From the book ** JavaScript Patterns by Stoyan Stefanov** covers your questions in detail. Here's a quote from the book on this subject:

So it could happen that a function A, being an object, has properties and methods, one of which happens to be another function B. Then B can accept a function C as an argument and, when executed, can return another function D.

Lastly, I would like to give very basic example. Everyone knows that arrays have different methods.

EXACTLY, different METHODS not FUNCTIONS. Like push(), pop(),slice(),splice()`.

Why they are methods?

They are methods because this functions are an part of an object, so as per point B, There environment is object, so they are termed as METHODS.

Cheers!!

For more such update follow @msabir

Top comments (5)

Collapse
 
msabir profile image
imsabir

@thumbone, Maybe I think I had clear your doubts now. Terminology changes based on scope of an object.

Collapse
 
thumbone profile image
Bernd Wechner

Again, provide an example please.

Collapse
 
msabir profile image
imsabir

There is two example mentioned one is OurObject code and another is func. If you want to know how to call that then
func is called by var someFunc = func('test','test', 'some other arguments of your choice')
and OurMethod is called by const calling_from_object = OurObject.OurMethod().

Thread Thread
 
thumbone profile image
Info Comment hidden by post author - thread only accessible via permalink
Bernd Wechner

I'm afraid I see precisely zero change in terminology anywhere there. And I think you writing neds lsightly better Quality Control:

So, does it means that every function is a *method * but every *method * is not a function ?

This is precisely the opposite of the truth, which would read much rather:

It means that every method is a *function * but not every *function * is a method

Further the appearance of the function as a property of the method has nothing to do with scope. You migth call that conetxt, but not scope. That is a function, that appears as a property of an object is called a method.

There is no change in terminology at all. In fact if we were to refer to OurMethod from within the scope of the object then it would be referred to with that scope made clear, as this.OurMethod().

They are methods because this functions are an part of an object, so as per point B, There scope is object,

This is just plain wrong, I am sorry. Their scope is not object, these methods enjoy the scope that the object enjoys, that is to scope of th emethod has barly any meaning. It is simply referred to a OurObject.OurMethod in any scope that the object is visible in and is referred to as this.OurMethod from within other methods of the object. Where this is not a change in terminology, it is simply the variable that contains a reference to OurObject.

I still see no change in terminology anywhere. And I wonder if we simply have a language problem. That you mean to say that the means of referring to a given function changes based on context, and are saying that terminology changes based on scope. The former is true, the latter probably not (I still cannot think of na example nor can I think of nay good reason it would change) and it may be that the two are trying to say the same thing through a language barrier (as I suspect English is not your first language). That would, if true, explain everything for me. As I am still puzzled as why you think terminology changes with scope.

Collapse
 
gjaryczewski profile image
Gerard Jaryczewski • Edited

Hello, @msabir ! You have a passion for programming and learning - it's visible. Keep going! :-)

I have read your post twice, and still don't know what's the goal?

The first sentence of the MDN quote looks like the answer for the question defined in the title, it's clear and simple: "A method is a function which is a property of an object". But the first bold text, which looks like your main point of interest, is formulated oppositely. Why? What's the reason behind this?

Maybe I don't understand, but IMHO you need to rethink this article and its structure, its logical flow. There are so many details in your sentences. Let's try to give a title for every paragraph, to get the big picture view. Does this text have a logical, consistent flow?

Writing clearly and logically, especially in a foreign language, is hard - believe me, I am Polish, and I wrote thousands of pages in Polish and English :-)

Keep going, cheers!

Some comments have been hidden by the post's author - find out more