2016-05-16 68 views
0

可能将这个标题命名得不好,会尝试并更好地解释一下。使用JS函数来使用另一个的语法? node.js

基本上我一直试图做一个函数:

var readlineSync = require('readline-sync'); 
function i(context) { 
    readlineSync.question(context) 
} 

var Username = i("Testing the prompt: ") 
console.log(Username) 

我觉得有写readlineSync.question一遍又一遍,而有刺激性,但在运行代码返回此:

Testing the prompt: Hello 
undefined 

上午我做错了什么?

+0

你知道写是不是拼写赖特? – thatOneGuy

+0

不是吗?它的正确... – Tom

+0

是的,我犯了一个错字。我的错。编辑之前。 – Tom

回答

3

你没有从函数返回任何东西。

它应该是:

function i(context) { 
    return readlineSync.question(context) 
} 
+0

啊,这是有道理的。谢谢! – Tom

2

你可能只是这样做:

var i = readlineSync.question 

// usage 
i('Testing the prompt: ') 

创建功能

的别名,或者,如果您使用的是ES6能环境(节点6或Chrome):

import { question as i } from 'readline-sync' 

// usage 
i('Testing the prompt: ') 

这是相同的:

var i = require('readline-sync').question 

// usage 
i('Testing the prompt: ') 
+0

啊,谢谢!我一直在试图弄清楚如何做确切的事情。 – Tom

1

你忘了return语句

function i(context){ 
return readlineSync.question(context) 
} 
相关问题