2016-10-01 74 views
0

我想知道是否有可能检查函数调用是返回到变量定义还是全局作用域。检查函数调用是否是变量定义

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 

该语法可能吗?

+0

我的答案仍然准确。 'RPC'不能说明它的返回值是如何使用的,语法是** not ** possible。您正试图以无法使用的方式使用JavaScript。此外,如果'RPC'使用AJAX获取值,则该值甚至无法返回。您必须返回一个承诺或将一个回调函数传递给'RPC'。 – meagar

回答

4

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

+0

有没有其他的选择? – user6907394

+3

@ user6907394不,没有。你需要解释你想要做什么。不知道你的*实际问题是什么,我们不能提供解决方案。 – meagar

+0

我尽力解释我现在的问题。 – user6907394