node js getters and setters code example
Example 1: getter and setters in js
var person={firstname:"chetha",secondname="kumar",get fullname()
{
return this.firstname+" "+this.secondname;
}
document.write(person.fullname);
var person={firstname:"chethan",secondname="kumar",course:"",set course(x)
{
this.course=x;
}
person.course="bca";
document.write(person.course);
Example 2: js why to use getters and setters
1) Syntax reasons. It’s easier and faster to read code
created with accessor functions
2) Encapsulation. I can create safer code with accessor functions.