Is this a constructor pattern (when I search the web it doesn't seem like it)?

I’m going through a course and it talks about the constructor pattern. The instructor used a car object as an example but I thought it was similar to react and so that’s the naming convention I’m using

class Component {
    constructor(props){
        // this.name = props.name
        for(const p in props){
            this[p] = props[p]
        }
    }  
}

According to my google research that’s where the constructor pattern ends (And I always thought so - though I’m a hobbyist beginner) but this instructor (Emmanuel Henri) says the constructor pattern is when we extend the class.

class App extends Component {
    constructor(props){
        super(props)
        console.log(props)
    }
}
const app = new App({name:"Bob", last: "Marley"}) 

Two questions.

  1. Is this a constructor pattern or just extending classes.
  2. Just for fun. in react the App.render() would that be a method within the Component class and called within the App?

I would have thought going with descriptive names would be preferable class Musicians or class Artists. What if you need multiple classes or/and need to extend extended classes like App? — you have kind of run out of names with your chosen convention.

I’m not an expert when it comes to design patterns and best practice, but you can substitute your for in loop with Object.assign(this, props). MDN Object Assign

Cool, I used the App and Component just to illustrate what I was trying to do.

Object.assign(this, props) - that’s cool it works

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.