首页 > 其他分享 >evalFn 字符串转执行函数 附带JSONParse函数

evalFn 字符串转执行函数 附带JSONParse函数

时间:2023-12-06 15:36:47浏览次数:30  
标签:function return 函数 indexOf JSONParse evalFn const

  const evalFn = (fn) => {
    var Fun = Function // 一个变量指向Function,防止前端编译工具报错
    return new Fun('return ' + fn)()
  }
/**
 * * JSON反序列化,支持函数和 undefined
 * @param data
 */
  const JSONParse = (data) => {
    return JSON.parse(data, (k, v) => {
      // 过滤函数字符串
      if (excludeParseEventKeyList.includes(k)) return v
      // 过滤函数值表达式
      if (typeof v === 'string') {
        const someValue = excludeParseEventValueList.some(excludeValue => v.indexOf(excludeValue) > -1)
        if (someValue) return v
      }
      // 还原函数值
      if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('=>') > -1)) {
        return evalFn(`(function(){return ${v}})()`)
      } else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) {
        const baseLeftIndex = v.indexOf('(')
        if (baseLeftIndex > -1) {
          const newFn = `function ${v.substring(baseLeftIndex)}`
          return evalFn(`(function(){return ${newFn}})()`)
        }
      }
      return v
    })
  }

测试代码

let a2 = new Function(`return function () {
const a = {
b: '333'
}
return a }()`)()

标签:function,return,函数,indexOf,JSONParse,evalFn,const
From: https://www.cnblogs.com/pengchenggang/p/17879646.html

相关文章

  • 字符指针及其函数的使用
    字符指针一、字符指针的有关内容首先需要明确的是,字符串实际就是字符数组。比如说:charp="helloworld";实际上应该是:p={'h','e','l','l','o','','w','o','r','l','d','\0'......
  • 多重继承下的虚函数调用
    C++中虚函数调用采用所谓的虚函数表(vtable)实现,对于简单的单继承,其实现如下图所示:(其中ClassA为ClassB的基类,详见深入浅出MFCP68)你也许会想到:C++支持多继承,在多继承的情况下,vatble以及内存布局该如何实现?以下也许就是你想要的答案代码:C继承于A和B,运行环境VC6.0classA......
  • Guava中的函数式编程
    第1章:引言大家好!今天小黑要和咱们聊聊,在Java中使用Guava来进行函数式编程。首先,让我们来聊聊什么是函数式编程。简单来说,函数式编程是一种编程范式,它将计算视为函数的评估,避免使用程序状态和可变数据。在函数式编程中,函数是“一等公民”,意味着它们可以像任何其他数据一样被传递......
  • 无涯教程-Erlang - list_to_atom函数
    此方法用于将列表项转换为原子。list_to_atom-语法list_to_atom(listvalue)listvalue - 这是需要转换为原子的列表值。list_to_atom-返回值基于列表值输入的原子。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("~p~n",[list_to_atom("a......
  • 无涯教程-Erlang - is_atom函数
    此方法用于确定术语是否确实是原子。is_atom-语法is_atom(term)term - 这是需要判断其是否为原子的值。is_atom-返回值如果条件值是一个原子,则返回true,否则将返回false。-module(helloLearnfk).-export([start/0]).start()->io:fwrite(atom1),io:fw......
  • 无涯教程-Erlang - is_dir函数
    此方法用于确定目录是否确实是目录,此方法是filelib库的一部分。is_dir-语法is_dir(directoryname)directoryname - 这是目录名,是否为目录名。is_dir-返回值是的,如果目录存在并且确实是目录。-module(helloLearnfk).-export([start/0]).start()->io:fwrit......
  • 深度掌握TypeScript中的重载【函数重载、方法重载】
    深度掌握TypeScript中的重载【函数重载、方法重载】1.函数重载,方法重载的重要性著名前端流行框架底层都用到函数重载,例如:Vue3底层源码就多处使用到带泛型的函数重载。很多前端面试更是拿函数重载作为考核求职者TS技能是否扎实的标准之一,如果你不掌握函数重载,等于你的TS技......
  • 模板函数
    引入现在要实现一个比较函数cmp,要求实现int,double,string(按字典序)的大小比较。一般来讲,常用的做法是写4个重载函数,分别对应4种不同的实参类型。不想这么麻烦?似乎可以使用无类型指针强行对这四种类型做适配。但是无类型指针每次调用之前必须显式地告诉它数据类型,但是这个......
  • 无涯教程-Erlang - copy函数
    此方法用于复制现有文件。copy-语法copy(source,destination)source     - 需要复制的源文件的名称。destination -文件的目标路径和名称。copy-示例-module(helloworld).-export([start/0]).start()->file:copy("Newfile.txt","Duplicate.......
  • python函数随笔1
    1.type(a)返回变量a的数据类型;2.列表b=[]print(b)b.append(4)print(b)b.append(5)print(b)b.append(True)print(b)b.append(False)print(b)b.append('False')print(b)[][4][4,5][4,5,True][4,5,True,False][4,5,True,False,'Fals......