将字符串转化为变量名的几种方法

参考:https://segmentfault.com/a/1190000017114522

头条面试题:实现一个get函数,使得下面的调用可以输出正确的结果
const obj = { selector: { to: { toutiao: "FE Coder"} }, target: [1, 2, { name: 'byted'}]};

get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name');
// [ 'FE Coder', 1, 'byted']
简而言之,这就是一个将字符串转化为变量名的题目

因为obj.selector.to.toutiao ,obj.target[0] ,obj.target[2].name能输出为’FE Coder’, 1, ‘byted’
那么obj.‘selector.to.toutiao’ ,obj.‘target[0]’ ,obj.‘target[2].name’ 能输出吗?显然不能

所以当我们把’selector.to.toutiao’转化为obj.selector.to.toutiao不就完美的解决啦

将字符串转化为变量名的方法:
  • 方法一:使用eval()函数

1.定义 : eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。(w3c解释:http://www.w3school.com.cn/jsref/jsref_eval.asp)
2.用法 :eval(“2+2”) ,返回4。
ps:注意,只接受原始字符串作为参数,也就是说

var xy=4;
console.log(eval(x+"y"));

会报错,而不是等于eval(“xy”)
image.png
3.使用eval()的解题办法

const obj = { selector: { to: { toutiao: "FE Coder"} }, target: [1, 2, { name: 'byted'}]};
  // [ 'FE Coder', 1, 'byted']
  //get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name');
  function get(obj,x,y,z){
    //把字符串变为变量
    x=(eval("obj."+x));
    y=(eval("obj."+y));
    z=(eval("obj."+z));
    console.log(x,y,z);
  }
  get(obj,'selector.to.toutiao','target[0]','target[2].name')
  • 方法二:使用模板字符串 ``

1.说明 : (参考http://es6.ruanyifeng.com/?search=new+function&x=9&y=10#docs/string)
模板字符串中嵌入变量,需要将变量名写在${ }之中

2.使用模板字符串的解题办法

function get(data, ...args) {
    const res = JSON.stringify(data);
    return args.map((item) => (new Function(`try {return ${res}.${item} } catch(e) {}`))());
  }

  const obj = { selector: { to: { toutiao: "FE Coder"} }, target: [1, 2, { name: 'byted'}]};
  console.log(get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name', 'asd'));

其他说明:
(1)get(…args) : …args为剩余参数语法,允许我们将一个不定数量的参数表示为一个数组。
(2)JSON.stringify : JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。
(3)Array map() :方法按照原始数组元素顺序依次处理元素后返回一个新数组
参考(http://www.runoob.com/jsref/jsref-map.html)
(4)new Function():
注意:(a) new function(arg1, arg2, …, argN, function_body)的参数一定要是字符串
(b).new Function()返回值为对象 ,而function(){ }返回的是函数的值(有new返回的一定是对象,无new返回函数的值(原始始类型或对象))

最最最重要的总结

既有变量名又有字符串,且都要变成变量名时 ,要先都转化为字符串

小白不知道总结的对不对 , 只能凭我现在的经验总结 , 以后随时更新…

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值