自定义的解释函数
当新建 repl.REPLServer
时,可能会提供自定义评价函数。
例如,这可用于实现完全定制的 REPL 应用程序。
以下说明了一个 REPL 的假设示例,该示例执行将文本从一种语言翻译成另一种语言:
const repl = require('node:repl');
const { Translator } = require('translator');
const myTranslator = new Translator('en', 'fr');
function myEval(cmd, context, filename, callback) {
callback(null, myTranslator.translate(cmd));
}
repl.start({ prompt: '> ', eval: myEval });
When a new repl.REPLServer
is created, a custom evaluation function may be
provided. This can be used, for instance, to implement fully customized REPL
applications.
The following illustrates a hypothetical example of a REPL that performs translation of text from one language to another:
const repl = require('node:repl');
const { Translator } = require('translator');
const myTranslator = new Translator('en', 'fr');
function myEval(cmd, context, filename, callback) {
callback(null, myTranslator.translate(cmd));
}
repl.start({ prompt: '> ', eval: myEval });