5

How to add a property to prototype with class expression. I can do it with the older method:

function Example(){}
Example.prototype.property = 'PROPERTY'

But I don't know how can I do the same thing with class expression

4
  • 2
    class is only synthetic sugar so under the hood it works as in Your example Commented Mar 29, 2020 at 18:58
  • 2
    You cannot do it inside the class body, there is no syntax for that. Only for defining methods (and getters/setters). Commented Mar 29, 2020 at 19:02
  • 2
    Usually, one does not set data values in the prototype unless they are read-only. If you ever set a new value for the data value in the prototype, it will affect all instances which is usually not what you want. If you ever a new value for the data value on an instance, it will "hide" and "override" the prototype value. It's way more common to just initialize a property on the object in your constructor and not put data on the prototype. Commented Mar 29, 2020 at 22:06
  • I actually wanted don't waste memory by big size properties during each instance process. The other hand, I wanted make that with class expression. But now I suppose there is no syntax for what I wish based on your answers and comments Commented Mar 30, 2020 at 10:32

1 Answer 1

2

Actually you can do it the same way :) enter image description here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.