I created a simple class that extends jQuery. It doesn't do much except add a couple of extra class-specific methods. The problem, these methods can't exist because the object converts itself into a type jQuery and the custom class prototype is gone.
This is what happens:
class myClass extends jQuery {
constructor(){
super("<div>")
this.append("<a>")
// This works as your would expect
}
createLink(){
this.find("a").attr("href", "//google.com")
}
}
let obj = new myClass();
obj.createLink() // Uncaught TypeError: obj.createLink is not a function
what's strange is that outputting the prototype to the console shows that .createLink is a function:
console.log(myClass.prototype) // {constructor: ƒ, createLink: ƒ}
What's even more strange, the object doesn't even seem to be an instance of itself:
class myClass extends jQuery {
constructor(){
super()
}
}
console.log(new myClass instanceof myClass) // false
But anything else works:
class myNewClass extends anyOtherObject {
constructor(){
super()
}
}
console.log(new myNewClass instanceof myNewClass) // true
The problem only exists when I try to extend jQuery. Why is that?