英文:
Nodejs how to create custom method for readline interface
问题
以下是您要翻译的代码部分:
如何为readline接口创建自定义方法?
myin = {}
myin.on = function (a,b) {
                
}
myin.resume = function () {
    
}
var rl = readline.createInterface({
      input: myin
  });
 rl.on('line', function (data) {
     console.log(data);	
 });
 
我想创建自己的输入方法,以供在readline中使用,就像调用`myin.on("data","aaaaa") ... myin.on("data","bbb\n")`时,readline捕获行..
但我找不到任何来源或文档..
请注意,我已经将代码部分翻译成中文,如您所请求。
英文:
How to create a custom method for readline interface ?
myin = {}
	myin.on = function (a,b) {
				
	}
	
    myin.resume = function () {
		
	}
	
	
	
var rl = readline.createInterface({
      input: myin
  });
  
 rl.on('line', function (data) {
     console.log(data);	
 });
I want to create my input method for using in readline
like when call myin.on("data","aaaaa") ... myin.on("data","bbb\n") readline grab the line
..
but I can't find any source or doc ..
答案1
得分: 0
I found that!
myin = {}
myin.on = function (a, b) {
    if (a == 'data') {
        myin.ondata = b;
    }
}
myin.resume = function () {
}
and then call like this:
myin.ondata("bbbb");
myin.ondata("aaaaa\n");
The output of rl.on('line') is like this: bbbbaaaaa
英文:
I found that !
myin = {}
    myin.on = function (a,b) {
                      if (a == 'data') {
					  
                       myin.ondata= b;    
					   
				 }
    }
    myin.resume = function () {
    }
and then call like this :
 myin.ondata("bbbb");
 myin.ondata("aaaaa\n");
and output of rl.on('line' is like this : bbbbaaaaa
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论