You are seeing the variable name with a property?
constructor(name){
name=this.name; <-- you have this reversed
}
You are referencing a property you never set
show(){
console.log(this.username) <-- what is username?
}
}
You are reading the value when the page loads
let name=document.getElementById('userName').value;
Basic idea using getter and setter
class Student {
_name = "unknown"
constructor() {
}
set name(name) {
this._name = name
}
get name() {
return this._name
}
reverseName() {
return [...this._name].reverse().join("");
}
}
const st = new Student();
document.getElementById("userName").addEventListener("input", function (e) {
st.name = e.target.value;
});
document.getElementById("show").addEventListener("click", function (e) {
console.log(st.name);
});
document.getElementById("rev").addEventListener("click", function (e) {
console.log(st.reverseName());
});
<label for="username">Name:</label>
<input type="text" id="userName">
<button type="button" id="show">Click</button>
<button type="button" id="rev">Reverse</button>
empdefined?emp.show()tost.show()sincestis what you declared.