How to define and use class with constructor in JavaScript ES6

1 Answer

0 votes
class CTest {  
    constructor(_name, _age){ 
        this.name = _name; 
        this.age = _age; 
    } 
  
    show(){ 
        console.log(this.name, this.age);
    } 
} 
const obj = new CTest("Tom", 46) 
      
console.log(obj.name);
console.log(obj.age);

obj.show(); 




/*
run:

Tom
46
Tom 46

*/

 



answered Mar 24, 2020 by avibootz

Related questions

1 answer 170 views
2 answers 240 views
1 answer 144 views
1 answer 197 views
...