首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查JavaScript中的参数名称,而不是值

检查JavaScript中的参数名称,而不是值
EN

Stack Overflow用户
提问于 2017-03-05 03:03:30
回答 1查看 98关注 0票数 0

我试图将if语句放在一个函数中,条件是函数中使用的参数的名称,而不是值。

可以使用什么条件来实现这一点?有可能吗?如果没有,有没有其他选择?

例如:

代码语言:javascript
运行
复制
var lorem="The name is Lorem, but the value isn't.";
var ipsum="The name is Ipsum, but the value isn't.";
//the values shouldn't matter

logIt(Lorem);

function LogIt(theName){
    if(**the name of the variable theName = "lorem"**){
    console.log("The variable 'lorem' was used.");
  }else if(**the name of the variable theName = "ipsome"**){
    console.log("The variable 'ipsum' was used.");
  }else{
    console.log("huh?");
  }
}
EN

Stack Overflow用户

发布于 2017-03-05 03:23:15

我认为在一般情况下不可能获得变量的名称,因为参数被复制到函数参数中。

也就是说,如果你只有一组固定的变量名,并且你正在使用ES6,你可以在技术上“破解”对象解构的问题:

代码语言:javascript
运行
复制
var lorem="The name is Lorem, but the value isn't.";
var ipsum="The name is Ipsum, but the value isn't.";

Logit({lorem}) 

function Logit({lorem, ipsum}) {
   if(lorem) console.log("Function called with lorem");
   else if(ipsum) console.log("Function called with ipsum");
   else console.log("Function called with something else");
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42600086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档