Confusing chapter "ES6: Use class Syntax to Define a Constructor Function"

  • This chapter needs more info about what is constructor function means ?

I’m coming from Java world and having difficulty to understand concept of constructor in JavaScript.

  • Chapter mentions This is to be noted, that theclasssyntax is just a syntax,, isn’t JavaScript supports OOP concepts ? In case it doesn’t why class keyword is used in first place ?

  • The exercise in this chapter has solution where class is created in function itself and this is entirely new concept which is not explained in chapter. Can you please add more details about class inside function concept ?

Thanks,
Vikram

This is for the benefit of the tests, and you can ignore everything outside of the class ... stuff that’s inside the function.

Note that you’ve already encountered this is you’ve covered functions, you just won’t have realised it. A function may return a value, so if you return a function from a function, then that’s going to be the value.

function example() {
  return function() {
    return "Hello";
  }
}

var myExample = example();
// The value assigned to myExample is a function
// I can now execute that function
myExample() // returns "Hello"

In this case, the thing that gets returned is a class, so the value is a class. It is not more complex than that, but the reason for doing it here is to do with allowing the tests to work.

Um, yes JS supports a form of OO, but it the way it works is not the same as Java OO. classes are used, practically, in a similar way, but they are just syntactic sugar over how JS inheritance works.

JS uses a technique called prototypal inheritance: objects [can] share a prototype object, on which methods are defined. And if you define new object types, they share the methods on that prototype plus those you define on their prototype and so on.

For example, Array is a type of Object, so it gets all the methods Object has, plus a load more that are shared amongst all Arrays.

Classes aren’t really anything special in JS: there is a function that, when used with the new keyword, creates a type of object with some defined properties out of thin air (this is the constructor). And you can add methods to the prototype of that type of object.

class MyClass {
  // This is the function that gets called when
  // you do `new MyClass("hello")`
  // It will create an object of the type `MyClass`
  // That look like this:
  // `{ foo: "hello" }`
  constructor (foo) {
    this.foo = foo;
  }
  
  // This is a function that gets added
  // to the prototype, so every
  // instance of `MyClass` can `sayFoo()`
  sayFoo() {
    return "say " + this.foo;
  }
}

I’d probably go through the OO section before the ES6 section, it may make more sense. Also don’t try to directly apply what you know about OO from Java to JS, it’s likely to not work great

Thanks a ton for detailed clarifications.

Unlearning existing language concepts will take some time for sure :slightly_smiling_face:

Did you learn JavaScript as your first programming language or something else ?

Regards,
Vikram

1 Like

Yeah, it’s…a bit wierd I would say. I have the opposite problem, I find classical OO languages a bit strange (not just because of JS), going into codebases where everything is not modules and functions always takes me quite a while to adjust to.

Sort of - I used PHP for a fair while when I was just starting out, but I was fumbling around at that point, and I would say that JS was the first language I actually learned properly.

1 Like

Thanks again for your frank opinions