栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建帮助函数以在隔离的范围内运行函数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建帮助函数以在隔离的范围内运行函数

我写了一个版本,isolated()可以处理任何非bind用户定义的函数表达式,并为范围访问抛出自定义错误:

function isolated (fn) {  return new Function(`    with (new Proxy({}, {      has () { return true; },      get (target, property) {        if (typeof property !== 'string') return target[property];        throw new ReferenceError(property + ' accessed from isolated scope');      },      set (target, property) {        throw new ReferenceError(property + ' accessed from isolated scope');      }    })) return ${Function.prototype.toString.call(fn)}  `).call(new Proxy(function () {}, new Proxy({}, {    get() { throw new ReferenceError('this accessed from isolated scope'); }  })));}// test functions[  () => arguments, // fail  () => this, // pass, no way to intercept this  () => this.foo, // fail  () => this.foo = 'bar', // fail  () => this(), // fail  () => new this, // fail  h => h, // pass  h => i, // fail  (a, b) => b > a ? b : a, // pass].forEach(fn => {  const isolate = isolated(fn);  console.log(isolate.toString());  try {    isolate();    console.log('passed');  } catch (error) {    console.log(`${error.name}: ${error.message}`);  }})

与尝试解析用户定义函数的参数和主体相比,此实现稍微简单一些,因此不容易出错。

该with语句是一种相对简单的方法,用于在强制隔离的函数中捕获所有作用域的引用并抛出

ReferenceError
。它通过
Proxy
使用一个
get
陷阱将一个中间变量插入到作用域中来实现,该陷阱会拦截所访问的作用域变量名。

Proxy
作为函数的背景是,这是一个有点棘手来实现,也残缺的唯一部分传递。这是必要的,因为
Proxy
提供给
with
语句的作用域不会拦截对
this
关键字的访问,因此还必须显式包装上下文,以拦截并间接使用
this
隔离的箭头函数内部的任何间接用法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号