Are functions methods of the global object?

I’m reading in YDKJS about how constructors(new) can confuse people into thinking Javascript has classes. Check out the code and text snippet below:

function Foo() {
    // ...

var a = new Foo();

The authors says

“…it appears that we are in fact executing a constructor method of a class, because Foo() is actually a method that gets called, just like how a real class’s constructor gets called when you instantiate that class.”

I didn’t understand this at first, but I guess this could make sense if you consider a function a method of the global object, referenced with this. For example, this.Foo(); Is my understanding correct?

You can view functions as methods of a global object, but I don’t think it contributes to what authors claiming. (Besides, although methods are functions, they are conceptually more tied to an object. So I wouldn’t call functions as a method of global object; they are just functions.)

Well, to some traditional OOP programmers, Foo will look like constructor method because it is used with ‘new’ keyword. So, Foo actually being a method or function or whatever, has nothing to do with the confusion.

For example,
traditional OOP language often instantiate its object like this

new Foo() // Foo is constructor method

Unfortunately, JS also has the same syntax

new Foo() // Foo is just a function (though, conceptually, it can still be thought of as a constructor)

So, you can sort of see how new programmers coming from traditional OOP language might think that JS has a class.

1 Like

Thanks for the reply. I definitely got the bigger picture, that is, things are not copied in JS with new, but rather new objects are created that can access properties higher on the prototype chain.

I know to someone coming from a class-based language that this can look like a constructor method, but here he’s saying that it actually is a method. It doesn’t make sense to me because it’s a function declared in the global scope. There’s also no property access with . or [].

Based on preceding words like ‘it appears that…’, I think the author is trying to describe the situation as a programmer coming from traditional OOP language rather than to declare that these functions are methods.

On the other hand, author could’ve thought it as method because functions defined in global scope can be thought of as members of global scope; for example, you can access them with this.myFn.

I wouldn’t get pedantic about rigorousness of author’s term choice. Probably because author’s point is clearly delivered to me. What happens when you perceive Foo() as a method or function? What difference does it make to what you’ve already understood?

Sometimes these terms are used interchangeably, although I don’t think they should be.

I wanted to understand it, but if it’s not important here then I’ll take your word for it. Thanks again!

Don’t count on my words but just think about it. What would be the worst thing that can happen from misunderstanding it? Anyway, sometimes things that doesn’t make sense becomes suddenly becomes clear after some times. Anyway, cheers