Gina
Gina

Reputation: 580

Are all functions in JavaScript essentially methods?

I'm relatively new to JavaScript and programming in general. I'm trying to understand more about how JavaScript works. Based on my understanding of methods and functions, and if I open my in browser console and type

typeof(this);
//=> "object" 
console.log(this); 
//=> Window {top: Window, window:Window, location:Location, external: Object, chrome: Object,...}

I am seeing the global object Window. Since subsequent functions are just methods from the host window object, aren't all functions essentially methods in JavaScript?

Thanks.

Upvotes: 2

Views: 852

Answers (3)

ramazan polat
ramazan polat

Reputation: 7880

Essentially the answer is NO, not all functions are methods in JavaScript.

Follow through:

var myMathObject = {
    pi: 3.14,
    add: function(a,b) {
        return a + b
    }
}
myMathObject.add(3,4) // 7

In the above example, add is a function and since it belongs to myMathObject, that makes it also a method. That's why we are referencing is with a dot between the object and the function as myMathObject.add. If you are in same execution context, you don't need to access the method with a dot.

function add(a,b) {
    return a + b
}
add(3,4) // 7
window.add(3,4) // 7
window.add == add // true

So the add function is also a method, a method of global object, in this case window.

Since we are always in a context of an object, that makes it impossible to declare a function outside an object in JavaScript, this leads us thinking that all functions are also a method but this is not correct.

ECMAScript defines method as "function that is the value of a property", According to this definition, if a function is not a value of a property, then it is not a method. That means anonymous functions are not methods. That makes sense since they are not accessible with dot notation. Also Immediately-invoked function expressions (IIFE) are not methods, because they are also anonymous functions.

(function (a,b) {
    return a + b
})(3,4) // 7

window. ? // how to access it?

That makes the answer clear: NO, not all JavaScript functions are methods.

Upvotes: 1

cookie monster
cookie monster

Reputation: 10972

ECMAScript defines method as a function that is accessed as the property of an object. So it's more how it's referenced that makes the difference than what it is.

ECMAScript 4.3.27
method

function that is the value of a property.


"Since all other subsequent objects and functions (which are objects) are just parasites from the host window, aren't functions methods in JavaScript?"

Don't know what you mean by "parasites", but as shown above, functions can be methods if they're the value of a property.

If you're talking about global functions, then that's a unique situation since they can be accessed as both variables and properties of the global object, so I'd say that whether you'd refer to it as a method would depend on how you're accessing it.


Don't rely on your logging of this. The way this works in JavaScript is such that it's a very dynamic value that isn't permanently bound to any particular object for a given method or function. (That is unless you used .bind() to create the function object.)

Upvotes: 3

Bill
Bill

Reputation: 3517

Yes, functions are methods in Javascript. There is no difference.

Upvotes: 2

Related Questions