var WebSocket = function(request, socket, body, protocols, options) {
this._driver = DRIVER.http(request, {maxLength: options.maxLength, protocols: protocols});//得到一个http的driver
API.call(this, options);//初始化父类属性
};
util.inherits(WebSocket, API);//WebSocket继承API
var API = function(options) {
this._driver.on('open', function(e) { self._open() });
this._driver.on('message', function(e) { self._receiveMessage(e.data) });
this._driver.on('close', function(e) { self._beginClose(e.reason, e.code) });
this._driver.on('error', function(error) {
};
请看上面代码,Websocket继承了API,但是在Websocket构造方法内又调用API.call(this, options);把自身传进去给API,在API的构造方法内又使用了websocket的_driver 属性。这在我以前java没遇到这情况,想搞清楚这两个类是什么关系?互为父类?如果API是父类,为何能调用子类的属性?很混乱,求大神解答下