1

I am learning Javascript on Codecademy. As my understanding Method is a function associated with the objects. I think to call it a method it should inside an object. Is it? Should I really fully understand main difference between functions and methods to write error free code. This is very confusing to me.

Below it the Codecademy code, they say on line 2 'setAge' is a Method where it is clearly looks like a function. It is not even related to any Object yet. Coz it is not even inside of any object.

// 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;
bob.setAge = setAge;

// make susan here, and first give her an age of 25
var susan = new Object();
susan.age = 25;
susan.setAge = setAge;
susan.setAge(35);
// here, update Susan's age to 35 using the method
Dhanu AT
  • 29
  • 4
  • 1
    It is technically a function that is later used as a method. – Ates Goral May 25 '16 at 02:51
  • I think there is something missing from your example. – Robert Moskal May 25 '16 at 02:52
  • It's also an anonymous function referenced via the variable `setAge` rather than a function named `setAge`. That whole question is really awful and confusing. – Marty May 25 '16 at 02:53
  • possible duplicate of [Method vs Functions, and other questions](http://stackoverflow.com/q/15285293/1048572). See also the generic [Difference between a method and a function](http://stackoverflow.com/q/155609/1048572) – Bergi May 25 '16 at 02:57
  • the difference between the two is how you invoke them. without bind/call/apply (or a 2nd arg to map/filter/etc), `this` inside the function refers to the object of which the function is a property, which makes it a method of `this`, or if `this` is undefined, then you know you have a non-method. it aligns with global functions being methods of `window` (technically, but nobody says that...) basically, the only non-methods in JS are private (nested) functions, however any `this`-using function can be used as a method. – dandavis May 25 '16 at 04:55

2 Answers2

1

That's a good question, I could see how that could be confusing. It appears that they are referring to it as a method because they later modify the bob object to include the function, thereby making it a "method".

var setAge = function (newAge) {
  // using "this" sometimes indicates
  // that a function may instead be an object method
  this.age = newAge;
};

// becomes a method
bob.setAge = setAge;

Their code is equivalent to the following:

bob.setAge = function (newAge) {
  this.age = newAge;
};

You are right in your understanding about methods.

Himmel
  • 3,425
  • 3
  • 29
  • 65
  • So if we use a function like bob.function(); it becomes a Method? Thanks – Dhanu AT May 25 '16 at 02:56
  • Yes, if you run `typeof bob`, you get `object` and if you run `typeof bob.setAge` you get `function`. We refer to functions that are properties of objects as "methods". – Himmel May 25 '16 at 02:57
  • @DhanuAT: Exactly that's how it works. A function that is invoked *on* an object is called a "method". – Bergi May 25 '16 at 02:58
0

As others have said, a method and a function have a lot in common, in fact all methods are functions.

The biggest difference is that a method is attached to an object and the this value gets set when that object is called.

I've made a snippet to demonstrate

"use strict";

var bob = {
  bool: true
};
bob.test = function() {
  console.log("this = ", this);
};

console.log("Here is bob test and 'this'");
bob.test();

// Separate the method
var tmp = bob.test;
// In strict mode, this will be undefined.
// In non-strict, it will be the same as `window`
tmp();
Jeremy J Starcher
  • 21,760
  • 5
  • 48
  • 70
  • A method doesn't need to be attached to an object. It can also be inherited, or completely standalone: `setAge.call(susan, 15)`. – Bergi May 25 '16 at 03:00