Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!