1. 首页
  2. 考试认证
  3. 其它
  4. recoil使用`require`模拟全局对象属性

recoil使用`require`模拟全局对象属性

上传者: 2024-12-11 14:02:13上传 ZIP文件 5.87KB 热度 1次

Recoil.js 通过 require 调用模拟全局对象的属性。一个常见用例是测试 polyfill。考虑如下代码:

foo.js


module.exports = { 

  isArray: Array.isArray || 'polyfill', 

  forEach: Array.forEach || 'pony' 

};

若要获取在 polyfill 中的字符串 isArray,可以这样做:

1.js


var opts = { obj: Array, key: 'isArray', val: null };

var foo = recoil('./foo.js', opts);

console.log(foo.isArray);  // => 'polyfill'

Recoil 的工作原理如下:它会保存并模拟对全局对象属性的访问,确保可以根据提供的 polyfill 或默认值返回正确的结果。

下载地址
用户评论