我想知道是否有可能检查函数调用是返回到变量定义还是全局作用域。检查函数调用是否是变量定义
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
该语法可能吗?
我的答案仍然准确。 'RPC'不能说明它的返回值是如何使用的,语法是** not ** possible。您正试图以无法使用的方式使用JavaScript。此外,如果'RPC'使用AJAX获取值,则该值甚至无法返回。您必须返回一个承诺或将一个回调函数传递给'RPC'。 – meagar