Use class to define constructor function
ES6 provides a new syntax to create objects, using the class keyword.
ES5
var Student = function(name){this.name = name;}var s1 = new Student('shreyash')console.log(s1)
Student { name: 'shreyash' }ES6
class Student {constructor(name) {this.name = name;}}var s1 = new Student('shreyash')console.log(s1)
Student { name: 'shreyash' }Use getter and setter to control access to an object
class Teacher{constructor ( teacher){this.teacher = teacher;}// getterget teacherName(){return this.teacher;}// setterset teacherName(changeTeacher){this.teacher = changeTeacher;}}const t1 = new Teacher('shreyash');console.log(t1.teacherName)t1.teacherName = "vaibhav";console.log(t1.teacherName)
shreyash vaibhav
Post a Comment