2 ms·
You don’t need to use the constructor syntax to have an object inherit prototypes from another.
by porphyrogene 8y ago
You don’t need to use the constructor syntax to have an object inherit prototypes from another.
- erikpukinskis 8y agoHow do you do it without a constructor? Iterate over the properties on the prototype?
- porphyrogene 8y agoThe global Object’s assign method is the “old” way to inherit prototypes. ES6 syntax did not add new language features; they were there all along.
- fiallega 8y agoThanks a lot for all clarifications.
- porphyrogene 8y agoNo problem. A good exercise I used to do was writing inheritence patterns first with pre-ES6 syntax, then constructors, then classes. They are, of course, interchangeable but it’s not wise to rehearse an anti-pattern. EDIT: I said assign method earlier, I meant create. The assign method is the pre-ES6 way of creating object instances. Object.create() is used to inherit prototypes. That’s such a common mix-up that I’m embarrassed I didn’t catch it.
- erikpukinskis 8y agoI just don’t use ES6. It’s amazing. I have such a smaller language surface, and so much more predictable and debuggable code. Not a popular approach though. (Of course I can and do use ES6 when coding for other people, I write to whatever standard is established in a codebase.)