recoil使用`require`模拟全局对象属性
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 或默认值返回正确的结果。
下载地址
用户评论