0

I am having trouble understanding prototypal inheritance when using constructors. I reached this lesson in The Odin Project where it says:

If you’re using constructors to make your objects it is best to define functions on the prototype of that object. Doing so means that a single instance of each function will be shared between all of the Student objects. If we declare the function directly in the constructor, like we did when they were first introduced, that function would be duplicated every time a new Student is created.

function Student() {
}

Student.prototype.sayName = function() {
  console.log(this.name)
}

I understand why you wouldn't want to duplicate the functions for every instance of the object, but I don't understand two things:

  1. Does this also apply to Classes which have constructors and Object() constructors?

  2. Why isn't this needed on other design patterns like Factory functions or the Module pattern? Wouldn't those functions also be duplicated for every instance of the object?

GeorgeCiesinski
  • 191
  • 1
  • 3
  • 11
  • 5
    When you use `class` syntax methods are automatically added to the prototype – Konrad Nov 23 '22 at 23:00
  • 3
    The Module pattern doesn't have multiple instances. – Barmar Nov 23 '22 at 23:03
  • What do you mean by "*Object() constructors*"? – Bergi Nov 23 '22 at 23:07
  • 3
    Yes, this is an issue also with factory functions. Sometimes it's mitigated by constructing an instance internally, sometimes it's solved by declaring shared functions outside of the factory and just creating properties that reference them, sometime it's just not cared about. – Bergi Nov 23 '22 at 23:08
  • @Bergi I linked the MDN docs to the [Object() Constructors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/Object) in my question. Sorry if this is a dumb question, I am still learning about constructors so this is part of my confusion. – GeorgeCiesinski Nov 23 '22 at 23:11
  • 2
    @GeorgeCiesinski I'm confused why you used the plural but linked the `Object` constructor in particular. So yes, when you create a `new Object()`, it inherits from `Object.prototype` and some methods are inherited from that. But you won't use `new Object` to define your own objects with your own methods? – Bergi Nov 23 '22 at 23:17
  • `new Object()` will do nothing if you pass object to it. It's just an old way to create an object or convert some primitive types to their object representations, just forget about it. – maksimr Nov 23 '22 at 23:32
  • @Bergi Okay I think I understand what you mean - I can create `new Object()` but I don't define functions within this syntax, and the prototype is undefined so I would define functions normally instead of under the prototype? – GeorgeCiesinski Nov 23 '22 at 23:33
  • 1
    @GeorgeCiesinski The prototype is not undefined (`Object.getPrototypeOf(new Object()) === Object.prototype`, as usual), but [you should not define your own methods on `Object.prototype`](https://stackoverflow.com/q/8341123/1048572). – Bergi Nov 24 '22 at 00:12

1 Answers1

0

Several people replied to my question but did not provide any answers. In order to close this question as answered, I compiled the information they provided here.

Classes:: Class syntax methods are automatically added to the prototype.

Object() constructor: Is not used to define objects with methods.

Module Pattern: The module pattern does not have multiple instances, so there is no instances to duplicate the functions.

Factory Functions: Are affected by this issue but there are ways to mitigate this. Check out the replies from Bergi which detail a few ways to mitigate it.

GeorgeCiesinski
  • 191
  • 1
  • 3
  • 11