JSFiddle - React, Tailwind, and code Playground
JavaScript
class A {
constructor(name, level) {
this.name = name;
this.level = level;
}
method1(){
console.log("this.name is ", this.name)
}
}
var a = new A('saurabh', 1)
// will show correct message as method is bind to object a which `this` refers to
a.method1()
// Uncommenting below code and calling methodWithoutBind would cause error as function is not bound to any object
/*
var methodWithoutBind = a.method1
methodWithoutBind()
*/
class B {
constructor(name, level){
this.name = name;
this.level = level;
}
}
var b = new B('mahesh', 2)
// here calling method would show appropriate name 'mahesh' as funtion is bind to object b, which has name property and hence it shows correct console
var methodWithBiding = a.method1.bind(b)
methodWithBiding()