1

I am learning about prototypal inheritance and I recently learned that if I use a factory function to create objects that all have methods, this results is unnecessary amounts of memory being used and that by setting the methods on the prototype, all objects generated from a factory function will share the same functions in memory. The same goes for a constructor function.

With that said, when I generated an object using the class syntax, the method1 and method2 methods by default are showing up in the prototype (__proto__) of the generated object. I was expecting to have to do manually like you would with a constructor function...

Test.prototype.get = somGetFunctionDefinedOutsideOfClass

in order to have the method1 and method2 methods set to the prototype.

Does this mean that if I generate 1 million objects all using the same Class, they all have their prototype set to the same methods and I wouldn't have to set the prototype manually?

const Test = class TestClass {
    constructor() {
        this.variable = 'testVariable'
    }
    method1() {
        console.log('method1')
    }
    method2() {
        console.log('method2')
    }
  } 

  const obj = new Test()
  console.log(obj)
  obj.method1() // logs: method1

Example using constructor function where methods are not on __proto__

  const Test = function TestObjectGenerator() {
    this.method1 = function() {
        console.log('method1')
    }
    this.method2 = function() {
        console.log('method2')
    }
  } 

  const obj = new Test()
  console.log(obj)
  obj.get()

1 Answer 1

1

Yes. Classes are just syntactic sugar over a "functional object". ES6 class methods are set to the prototype automatically which means they behave no differently to ES5 function declaration objects.

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

9 Comments

I am totally with you on functions defined outside the Class but I'm still not understanding the get and set methods defined within the Class. They are both showing up in proto so I am guessing that if I generated many objects from the Class, they'd all share the same prototype.
Yes they share the same prototype. ES6 classes are no different than regular ES5 function declared JavaScript classes. They behave in the exact same way. ES6 classes are simply a different syntax for creating regular ES5 JavaScript objects.
When I use a constructor function and do something like this.get = function() {}, the method shows up in the root of the object not __proto__, like it does when using a Class.
What exactly are you trying to achieve? get and set are keywords to apply a specific set of operations on a member variable before it's retrieved / modified. Your example doesn't make much sense as you don't specify a member variable after your get and set declarations. As for typical method declarations in ES6 classes, they are set on the prototype of the object like normal ES5 function class declarations are.
Yes. If you're using an ES6 class, functions declared within the class are automatically set onto the prototype.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.