DEV Community

imsabir
imsabir

Posted on

4 1

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
Bernd Wechner
Comment hidden by post author
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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay