Node.js如何为readline接口创建自定义方法。

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 23:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定