How to use constructor in class and create an object in JavaScript

3 Answers

0 votes
class Worker {
    constructor(id, name, age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    show () {
        console.log(this.id + ' ' + this.name + ' ' + this.age);
    }
}
  
const worker = new Worker(2345, 'Tom', 54);

for (let key in worker) {
    console.log(key, worker[key]);
}

  
  
  
/*
run:
  
id : 2345
name : Tom
age : 54
  
*/
 

 



answered Mar 4, 2020 by avibootz
edited Mar 5, 2020 by avibootz
0 votes
class Worker {
    constructor(id, name, age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    show () {
        console.log(this.id + ' ' + this.name + ' ' + this.age);
    }
}
  
const worker = new Worker(2345, 'Tom', 54);

worker.show();

  
  
  
/*
run:
  
2345 Tom 54
  
*/
 

 



answered Mar 5, 2020 by avibootz
0 votes
class Worker {
    constructor(id, name, age) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.show = function () {
            console.log(this.id + ' ' + this.name + ' ' + this.age);
        }
    }

}
  
const worker = new Worker(2345, 'Tom', 54);

for (let key in worker) {
    console.log(key, worker[key]);
}

  
  
  
/*
run:
  
id 2345
name Tom
age 54
show [Function]
  
*/

 



answered Mar 5, 2020 by avibootz

Related questions

...