0

I have always been confused between these two. From what I understand, methods always use dot notation like:

$( "li.third-item" ).nextAll().addBack().css( "background-color", "red" );

In this case, nextAll(), addBack() and .css() are all methods.

Similarly, functions are called directly. Like,

make_tea();

I want to know with certainty whether something is a function or a method by looking at how it is used. Does using PersonOne.run_now() always guarantee that run_now() is a method? Can run_now() still be a function if we are using a . to access it?

My question is "Does a function become a method when we are using . to call it?"

Neena Vivek
  • 667
  • 7
  • 19
  • 1
    JavaScript methods are the actions that can be performed on objects. A JavaScript method is a property containing a function definition. [src](http://www.w3schools.com/js/js_object_methods.asp) – Kevin Kloet Dec 21 '16 at 10:38
  • In Javascript, methods and functions are nearly identical -- like previous stated: methods are simply properties attached to objects which is described as a function (the only difference is that you attach methods to types or to variables, and functions are able to stand alone) – mike510a Dec 21 '16 at 10:39
  • 1
    In fact, all functions are methods because they are part of the window object i think – Jonas Wilms Dec 21 '16 at 10:40
  • @Jonasw: that assumes the javascript is running in a browser. As far as I am aware, js running in Node would not necessarily have a window object. – Sam Axe Dec 21 '16 at 10:42
  • 1
    @Jonasw Counter example: `function () { function foo() {}; }` – deceze Dec 21 '16 at 10:45
  • Are you thinking of the difference between a 'prototype' extension method and a function call? E.g. `Array.prototype.shout` vs shout(arr) https://jsfiddle.net/a6787yny/ – G0dsquad Dec 21 '16 at 10:47
  • @deceze: so in fact everything that is declared in an anonymous function is not part of an object right? – Jonas Wilms Dec 21 '16 at 10:48
  • @Jonasw Anything that is declared in any sort of function and is not attached to an object is not part of the global object. – deceze Dec 21 '16 at 10:49
  • @deceze ok ive got it. Thanks ;) – Jonas Wilms Dec 21 '16 at 10:49

1 Answers1

2

Methods are simply function references stored in an object property. Method in Javascript is merely a concept, not actually an existing syntactical part. Also, there is no method keyword in Javascript.

function foo() { /* whatever */ }

var bar = {};
bar.baz = foo;

// You'd consider this a function call
foo();

// While the following is actually syntactically also a function call
// you might consider baz a "method of the bar object"
bar.baz();
// and thus bar.baz() would then be a "method call".

Please not that to access a property of an object you have two possible ways: object.property and object[propertyName] where propertyName is a string containing the name of the property.

So

bar.baz()

achieves exactly the same as and is identical to

bar["baz"]().
connexo
  • 53,704
  • 14
  • 91
  • 128
  • Does using a `.` like I did in the question, guarantee that this particular function is actually a method? – Neena Vivek Dec 21 '16 at 10:45
  • Note that Javascript is extremely fluid in this area… `var f = o.f;`, `f.call(o)`… – deceze Dec 21 '16 at 10:45
  • Why would you even care? Syntactically, in JS there is no difference between a function and a method. – connexo Dec 21 '16 at 10:46
  • @connexo So, I can use both the terms interchangeably in JavaScript? – Neena Vivek Dec 21 '16 at 10:48
  • @Neena Vivek: every function that is declared inside of an object (the window object too) is a method. Just functions that are declared inside of functions (or methods) arent methods. – Jonas Wilms Dec 21 '16 at 10:51
  • 1
    A function that is stored in the property of an object is commonly referred to as a method of that object (that is, something that the object can perform). – connexo Dec 21 '16 at 10:52
  • Re your new example code: I would consider the latter a *method call*. How that method got attached to the object and whether it actually interacts with the object (uses `this`) is somewhat besides the point. If you're going to postulate that methods are "function references stored in an object", then it's a method. – deceze Dec 21 '16 at 10:53
  • @connexo: in your example foo() is equal to window.foo() ... – Jonas Wilms Dec 21 '16 at 10:53
  • @Jonasw Again, your scope is limited to the browser. JS exists outside of the browser as well. – connexo Dec 21 '16 at 10:54
  • But i bet this is the area the OP is mostly writing code... – Jonas Wilms Dec 21 '16 at 10:55
  • @deceze trying to be precise, I have added *method call* explanantion in code comments. – connexo Dec 21 '16 at 10:57
  • Well, *syntactically* it's a method call. Assume you have no prior knowledge how `bar` got assembled; `bar.baz()` is quite obviously a method call. (The distinction doesn't really matter though… :)) – deceze Dec 21 '16 at 10:59
  • I disagree here. Since there is no syntactical, lexical part of JS that is a `method`, it 's merely **conceptually** a *method call* , not *syntactically* . Syntactically it can only be a function call. – connexo Dec 21 '16 at 11:06