Object Oriented Programming in JavaScript Part – 2
Free Web development courses with real-time projects Start Now!!
Program 1
class Employee { constructor(eid=101,ename="Ashok",salary=5000) { console.log("This is constructor of class") this.eid=eid this.ename=ename this.salary=salary } showData() { console.log("Employee id ",this.eid) console.log("Employee name ",this.ename) console.log("Employee salary ",this.salary) } } E1=new Employee(501,"Rajesh Sharma",90000) E2=new Employee(502,"Amit Mishra",60000) E3=new Employee() E1.showData() console.log("--------------------------------------") E2.showData() console.log("--------------------------------------") E3.showData() // console.log("Employee id ",E1.eid) // console.log("Employee name ",E1.ename) // console.log("Employee salary ",E1.salary) // console.log("Employee id ",E2.eid) // console.log("Employee name ",E2.ename) // console.log("Employee salary ",E2.salary)
Program 2
class Myclass { constructor(n) { this.n=n } factorial() { let f=1 while(this.n!=0) { f=f*this.n; this.n-- } return f } evenodd() { if(this.n%2==0) console.log("Even Number ") else console.log("odd Number ") } } "use strict" const ps=require("prompt-sync") const prompt=ps({sigint:true}) let n=parseInt(prompt("Enter a number: ")) M1=new Myclass(n) console.log("Factorial is: ",M1.factorial()) M1.evenodd()
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google