io
is a factory rather than a constructor function and returns an object created by the Manager constructor.
The error message now implies that the constructor
function for Websocket
returns the object returned by the super
call, as distinct from the new object created when calling Websocket
as a constructor. This is easily confirmed:
var testObject = {};
function io() { return testObject;}
class Websocket extends io {
constructor(address) {
super(address)
}
message() {
alert('1')
}
}
let socket = new Websocket();
console.log( "socket same as testObject: " + (socket === testObject));
console.log( "Websocket.prototype.message: " + Websocket.prototype.message);
Running the snippet shows Websocket
returns the object returned by super
, which, since it is not the object created by new
, does not inherit methods from Websocket.prototype
where the message
method is sitting.
This calls for a rethink in design and perhaps further experimentation and learning about how JavaScript object prototyping really works. It also highlights the pitfalls of treating JavaScript as if it were just some yet-another class based language!