I have a simple class named B which have a basic property. I have a prototype method named print which will print that Class property. Now everything works fine if i use normal function syntax. I want to use arrow function syntax for my prototype am already aware of that this
inside arrow function will refer to global(window) object. I want this
to bind my Class. Is there any way how can i achieve it?
class B {
constructor() {
this.name ="kannan";
}
}
B.prototype.print = function(){
console.log(this);
console.log(this.name);
}
let name = new B();
name.print() //Prints my name and works correctly
However, if I try with arrow syntax
B.prototype.print = () => {
console.log(this);
console.log(this.name);
}
name.print() //prints this as a global window object
How can I achieve to print my name with arrow syntax?