Method vs Function in Javascript
imsabir

imsabir @msabir

About: I am web developer with expertise in cutting edge technologies, technical writer, technical recruiter. I believe in #Gratitude. ✨✨✨

Location:
Somewhere on Earth, Universe 🌎
Joined:
Feb 22, 2022

Method vs Function in Javascript

Publish Date: Mar 8 '22
15 5

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

Comments 5 total

  • imsabir
    imsabirMar 8, 2022

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

    • Bernd Wechner
      Bernd WechnerMar 8, 2022

      Again, provide an example please.

      • imsabir
        imsabirMar 8, 2022

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

  • Gerard Jaryczewski
    Gerard JaryczewskiMar 8, 2022

    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!

Add comment