Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

58
Views
JavaScript copy a class atributes and create a new identical object

I have a class that copy objects and it looks like this:

Class CoppyComponent {
    constructor(element) {
        return Object.assgin(Object.create(Object.getPrototypeOf(element)), element);
     }
}

I have another class to create HTML elements that use an array to keep track of the HTML elements created

Class Component {
    constructor(){
        this.arr = [];
    }
}

I use the classes this way:

const hi = new Component();
hi.CreateEl({ type: "h1", text: "Hello", parrent: ".a" });
hi.CreateEl({ type: "h2", text: "Hello", parrent: ".a" });
hi.CreateEl({ type: "h3", text: "Hello", parrent: ".a" });
hi.CreateEl({ type: "h4", text: "Hello", parrent: ".a" });
hi.CreateEl({ type: "h6", text: "Hello", parrent: ".a" });
const h1Copy = new CoppyComponent(hi);
`h1Copy.load(".b");`

createEl methid just create a new HTML element

load method just modify the parent element HTML elements go into div with class b only instead of both divs (a, b)

I console.log both objects (hi, h1Copy) and are identical What I think the problem is but I am not sure is that both objects share the same array.

7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.