1

Coming from a Python background, I've read this, learning Javascript:

Classes are in fact "special functions", and just as you can define function expressions and function declarations, the class syntax has two components: class expressions and class declarations.

on dev.mozilla website..

What I understand is that :

  • Functions are objects in OOP

  • And not classes are functions.

  • And maybe classes themselves are objects. I am not sure.

Am I wrong?

6
  • 3
    A JavaScript class declaration creates a constructor function, using the class name as its name. The contents of the class declaration initializes the constructor prototype and creates the actual body of the function. So, in effect, yes, a class declaration creates a function. Commented Sep 4, 2021 at 12:10
  • In JS, classes are functions and functions are objects. However, you have to use new when instantiating a class. You cannot call a class like a normal function. Commented Sep 4, 2021 at 12:14
  • Thank God. I'm sorry if I was a bit lazy about a little deep search. Now I've pretty much understood. I'm a very beginner in JavaScript, so I had to make a search to understand some of the terms you've used. Thanks! Commented Sep 4, 2021 at 12:47
  • @SebastianSimon But what is the purpose of class keyword if there is function? The console accepts it in FireFox. Commented Sep 4, 2021 at 12:59
  • 1
    @OmarAlHadidi See javascript: what's the difference between a function and a class. Do note the comments about outdated answers by T.J. Crowder. If you want something like classes you should always prefer the class keyword. If you want something like normal functions you should either use methods or arrow functions. A function can behave both as a normal functions or as a limited form of classes (e.g. subclassing native classes not fully possible, no private fields, …), and they’re no longer very useful (before ECMAScript 2015, it was the only kind of function). Commented Sep 4, 2021 at 13:11

1 Answer 1

2

Classes are indeed functions, and functions are also objects - you can put arbitrary key-value pairs onto functions, just like onto objects.

class X{} console.log(typeof X); console.log(X instanceof Object);

That's a class declaration. A class expression is like:

const TheX = class X{} console.log(typeof TheX); console.log(TheX instanceof Object);

When a class has key-value pairs directly on it (like an object), the properties are generally called "static":

class X{ static prop = 'foo'; } console.log(X.hasOwnProperty('prop'));

Classes created with class can't be invoked without new, but classes created with function can (in which case it's equivalent to a standard function).

function X() { } // As a class: const x = new X(); // As an ordinary function: const somethingElse = X();

With function syntax, whether the function behaves as a class or as a plain function is determined by the caller - by whether new is used or not. If new is used, this inside the function is set to be an object inheriting from X.prototype, which is automatically returned at the end. If new isn't used, this inside the function is set to the calling context if there is one (eg someObj.X() will have this be someObj).

Sign up to request clarification or add additional context in comments.

9 Comments

And the main difference between function and a class is that this in class points to class itself object, while in function it points to it's parent?
Kind of, the this is determined by how it's called. new X() calls X like a class, giving it a this of an object inheriting from X.prototype. X() (when X is declared with function syntax rather than class syntax) results in it behaving just like an ordinary function.
@OmarAlHadidi ... A class declaration could be more compared to an immediately invoked function expression which returns the constructor function. Thats the reason why a class declaration can not be hoisted.
@PeterSeliger They're not hoisted because (1) inline with let and const of ES6, it's not intuitive and would be bug-prone (2) class declarations can have side-effects (in their static properties)
@CertainPerformance Thanks, I got it. But I wanted to know.. Is it right to define a class, whether it is a declaration or an expression, with empty brackets (The curly ones)? Or you were writing it like this as an example only?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.