0

I know it's a very basic question, but since I am a beginner I have very limited knowledge about it. I tried to understand it by googling various online sources, but could not get a crystal clear view of it. Thanks in advance.

  • I would say function and method are synonyms in all languages, not just Javascript. There might be language lawyers here that will cite subtle differences, but that's how I think of them. – duffymo Dec 25 '15 at 04:28
  • @duffymo: How you think of them is immaterial. How [ES5](https://es5.github.io/#x4.3.27) and [ES6](http://www.ecma-international.org/ecma-262/6.0/#sec-method) specification thinks of them is the important bit. And there is a clear difference - only methods can be invoked on a target using the dot notation, setting `this` (or `self`, in other languages) in the process. – Amadan Dec 25 '15 at 04:31
  • You're correct; my thinking is immaterial here. Thank you for the instruction. – duffymo Dec 25 '15 at 13:35

1 Answers1

2

A method, in JavaScript, is a function that is set on an object property; no more, no less.

window.f = function() {} // method of `window`

a = {
  g: function() {} // method of `a`
};

function x() {
  var h = function() {} // not a method, because it's in a local variable,
                        // not in an object attribute
  var b = { i: h };     // method of `b` now.
};
Amadan
  • 169,219
  • 18
  • 195
  • 256