检查函数调用是否是变量定义

时间:2016-10-01 04:08:50

标签: javascript

我想知道是否可以检查函数调用是返回变量定义还是返回全局范围。

function getName() {
    if (!isVariableDefinition()) {
        console.log("John Doe");
    } else {
        return "John Doe";
}

var name = getName()
// name == "John Doe"

getName()
// Should print "John Doe" to the console

我有从服务器调用远程过程(通过AJAX)的代码。在服务器上,如果程序是变量定义,则应保存其名称以供其他用途使用,否则,应该只返回结果。

var result = RPC('getName')
// The server should receive "result" and the call

RPC('getName')
// The server should receive only the call

这种语法可能吗?

1 个答案:

答案 0 :(得分:4)

不,这是不可能的。函数不知道它返回的值是做什么的。

相关问题