Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method vs Functions, and other questions

With respect to JS, what's the difference between the two? I know methods are associated with objects, but am confused what's the purpose of functions? How does the syntax of each of them differ?

Also, what's the difference between these 2 syntax'es:

var myFirstFunc = function(param) {     //Do something }; 

and

function myFirstFunc(param) {     //Do something }; 

Also, I saw somewhere that we need to do something like this before using a function:

obj.myFirstFunc = myFirstFunc; obj.myFirstFunc("param"); 

Why is the first line required, and what does it do?

Sorry if these are basic questions, but I'm starting with JS and am confused.

EDIT: For the last bit of code, this is what I'm talking about:

// here we define our method using "this", before we even introduce bob var setAge = function (newAge) {   this.age = newAge; }; // now we make bob var bob = new Object(); bob.age = 30; // and down here we just use the method we already made bob.setAge = setAge; 
like image 563
Karan Goel Avatar asked Mar 08 '13 01:03

Karan Goel


People also ask

What is difference between methods and functions?

Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.

Should I use method or function?

Here's a simple rule of thumb: if the code acts upon a single instance of an object, use a method. Even better: use a method unless there is a compelling reason to write it as a function. Don't over think it.

What is the difference between a method and a function in what ways are they similar Why do you think Python makes a distinction between methods and functions?

Difference between Python Methods vs FunctionsMethods are associated with the objects of the class they belong to. Functions are not associated with any object. We can invoke a function just by its name. Functions operate on the data you pass to them as arguments.

What is the difference between a method and an object?

A method is a block of code that can be called by name within the program. An Object is an instance of a class. They may contain methods or attributes that define what they are.


2 Answers

To answer your title question as to what is the difference between a 'function' and a 'method'.

It's semantics and has to do with what you are trying to express.

In JavaScript every function is an object. An object is a collection of key:value pairs. If a value is a primitive (number, string, boolean), or another object, the value is considered a property. If a value is a function, it is called a 'method'.

Within the scope of an object, a function is referred to as a method of that object. It is invoked from the object namespace MyObj.theMethod(). Since we said that a function is an object, a function within a function can be considered a method of that function.

You could say things like “I am going to use the save method of my object.” And "This save method accepts a function as a parameter.” But you generally wouldn't say that a function accepts a method as a parameter.

Btw, the book JavaScript Patterns by Stoyan Stefanov covers your questions in detail, and I highly recommend it if you really want to understand the language. 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.

like image 113
mastaBlasta Avatar answered Sep 30 '22 10:09

mastaBlasta


There is a slight difference -

Method : Method is a function when object is associated with it.

var obj = { name : "John snow", work : function someFun(paramA, paramB) {     // some code.. } 

Function : When no object is associated with it , it comes to function.

function fun(param1, param2){ // some code... } 
like image 22
Pushpendra Singh Avatar answered Sep 30 '22 12:09

Pushpendra Singh