Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use factory function to create an object in JavaScript

1 Answer

0 votes
function createWorker(id, name) {
    return {
        id,
        name,
        show: function() {
            console.log(this.id + ' ' + this.name);
        }
    };
}
 
const worker1 = createWorker(2345, 'Tom');
worker1.show();
 
const worker2 = createWorker(9837, 'Dacey');
worker2.show();
 
 
 
/*
run:
 
2345 Tom
9837 Dacey
 
*/

 



answered Mar 4, 2020 by avibootz
edited Mar 5, 2020 by avibootz

Related questions

1 answer 151 views
1 answer 112 views
1 answer 97 views
97 views asked Mar 7, 2024 by avibootz
...