2

I am trying to really understand the javascript language. I am learning the differences between functions and methods.

This answer explains that methods are functions attached to objects: Difference between a method and a function

But this article from MDN says that

A value of property can be a function, which is then known as the object's method.

The sentence from MDN implies that JS objects have single methods. Is that correct? In other words: can javascript objects have multiple methods? Or is there a javascript concept roughly akin to "The Object's Method."

Community
  • 1
  • 1
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • 1
    How about you try it? Side note: there is no real difference between a function and a method in JavaScript. – freakish Jan 12 '14 at 16:42
  • You seem ultra focused on that one sentence. Read the entire article, and you'll have your answer. – cookie monster Jan 12 '14 at 16:43
  • @freakish a method can use the word this and it would refer to the objects context. If a function uses the word this, it refers to Object. Right? – bernie2436 Jan 12 '14 at 16:46
  • @akh2103 Not quite. If you have a function `fn` and an object `x` and you will do `fn.call(x);` then `this` becomes `x` (it doesn't matter whether `fn` is a method or a function). And that's what happens under the hood: `x.fn()` is equivalent to `fn.call(x)`. So there is no much difference. – freakish Jan 12 '14 at 16:49
  • @akh2103 Also if you have a function `fn` and you call it directly: `fn()` then `this` becomes `window`, i.e. `fn()` is equivalent to `fn.call(window)`. Read more about *call* [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) and about its sister function *apply* [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply). – freakish Jan 12 '14 at 16:57

5 Answers5

1

Javascript objects can certainly have multiple methods.

I find the quote less confusing if we preserve the original italics:

A value of property can be a function, which is then known as the object's method.

The "the" goes with "object", not "method".

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Probably would be less confusing if the quote was "... then known as AN object's method." For that matter, they could have left "object's" out altogether. – Joel Cochran Jan 13 '14 at 12:09
1

An object can have multiple attributes. Those attributes can contain functions. Functions are also objects, so they in turn also can have multiple attributes and so on.

Dima Knivets
  • 2,418
  • 7
  • 28
  • 40
  • Function constructors return Objects, but Functions are not Objects: each are distinct types in the JavaScript type system. – Joel Cochran Jan 13 '14 at 12:11
  • It's like saying that String class is not a class, it is String (in Java for example), but it is still a class. [A function created with the `function` statement is a `Function object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function#Description) – Dima Knivets Jan 13 '14 at 12:20
  • I was being overly simplistic: you are absolutely correct, Functions ARE Objects. They inherit from Function.prototype, which is why we have apply, call, bind, etc. If, however, you do a typeof over a function, the value will be "function", not "Object". – Joel Cochran Jan 14 '14 at 14:32
1

Javascript objects can have multiple methods, I think whoever wrote that article just worded it incorrectly, or a little ambiguously.

K. S.
  • 586
  • 5
  • 20
0

It can have multiple methods. But it can only have one value at a time. But you could even have multiple methods that each return the value...

Floris
  • 45,857
  • 6
  • 70
  • 122
0

JS objects can have multiple methods like

  • window.alert("");
  • window.confirm("");
  • window.prompt("");
Ahmed
  • 2,176
  • 5
  • 26
  • 40