所以基本上我有一个es6 js类,在这个类中创建构造函数变量,然后在类方法中赋值。之后,我创建了一个子类,它扩展了第一个类,并将我希望使用的所有变量传递给子类构造函数,然后传递给超级方法。在这一个变量正确且另一个变量未定义之后,将以相同的方式创建、传递和记录这两个变量。为什么它没有定义??
class socket {
constructor() {
this.socket = {};
this.base = '/*string*/';
this.mainIndex;
this.dashboard;
this.user_id;
}
socket_start() {
//do stuff here to start websocket
self.socket.onmessage = function ( event ) {
let stream = JSON.parse( event.data );
this.mainIndex = stream.url.mainIndex;
this.dashboard = stream.url.dashboard;
this.user_id = stream.staffInfo.id;
this.base = stream.base;
console.log( this.user_id ); // "1"
console.log( this.base); // "string"
}
}
}
class main extends socket {
constructor( user_id, base, socket, mainIndex, dashboard ) {
super( user_id, base, socket, mainIndex, dashboard );
console.log( this.user_id ); // undefined <--????
console.log( this.base); // "string"
}