为什么我不能从同一类中的另一个方法调用此方法
Why can I not call this method from another method in the same class?
在下面的代码中,我不知道如何从handler(sock)方法内部调用parseData(data)方法。
我已经尝试了其他问题的所有方法:其他问题
"use strict";
var net = require('net');
var HOST = '127.0.0.1';
class Sorter {
constructor(sorter) {
this.sorter = sorter;
console.log(sorter.name + ' Port: ' + sorter.port + ' running!');
this.initialize();
}
initialize() {
net.createServer(this.handler).listen(this.sorter.port, HOST);
}
handler(sock) {
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ':' + sock.localPort + ': ' + data);
parseData(data);
// Write the data back to the socket, the client will receive it as data from the server
//sock.write('Hello there'); //response
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}
parseData(data) {
// find out if I have a carton with the data recvd
console.log('looking for container: ' + data + ' ...');
console.dir(this.findByContainer(data));
}
findByContainer(container) {
return GLOBAL.CARTONS.filter(function( obj ) {
return +obj.container === +container;
})[0];
}
}
module.exports = Sorter;
使用this.parseData(data);
。这个必须被引用,因为类instinciation。请参阅方法内部的其他调用。
parseData
不在作用域中。当您从不同的作用域调用函数时,需要绑定函数,使它们处于作用域中。
当你打电话给this.initialize()
时,你的想法是正确的;它不是全局函数,因此需要参考this
。您需要对parseData
执行同样的操作。
但是,因为您是从回调调用parseData
,所以this
将不是您所期望的。您需要绑定回调或在回调之外保存对this
的引用。我更喜欢前者,但这取决于你。
使用bind
:
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ':' + sock.localPort + ': ' + data);
this.parseData(data);
// Write the data back to the socket, the client will receive it as data from the server
//sock.write('Hello there'); //response
}.bind(this));
使用保存的对this
:的引用
var _this = this;
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ':' + sock.localPort + ': ' + data);
_this.parseData(data);
// Write the data back to the socket, the client will receive it as data from the server
//sock.write('Hello there'); //response
});
您也可以为回调使用箭头函数。箭头函数的作用域是自动维护的,但仍需要引用this
才能调用parseData
。
使用箭头功能:
sock.on('data', data => {
console.log('DATA ' + sock.remoteAddress + ':' + sock.localPort + ': ' + data);
this.parseData(data);
// Write the data back to the socket, the client will receive it as data from the server
//sock.write('Hello there'); //response
});
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 使用clickToggle并在单击另一个元素时关闭元素
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 无法从jquery Mobile导航栏重定向到另一个页面
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 在Javascript中,你能在同一类的另一个函数中调用一个类的函数吗?
- 从同一类中的另一个方法调用 java 脚本类内的方法
- 从同一类中的另一个方法调用 javascript 类方法
- 如何使下拉菜单关闭,因为同一类的另一个下拉菜单在jquery / javascript中打开
- 为什么我不能从同一类中的另一个方法调用此方法
- 单击该类已添加,但单击另一个则不会删除已添加的同一类
- 传递$的结果.将getJSON转换为同一类的另一个方法
- 在同一类的另一个方法中调用一个方法