Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在生产代码中模拟JavaScript对象_Javascript_Proxy_Mocking - Fatal编程技术网

在生产代码中模拟JavaScript对象

在生产代码中模拟JavaScript对象,javascript,proxy,mocking,Javascript,Proxy,Mocking,我想在生产代码中模拟一个JavaScript对象,这样它就可以配置为不做任何事情。对象非常复杂,具有许多属性,包括嵌套属性和函数。访问任何属性都不应导致TypeError,并且不应有任何副作用。运行任何函数名都应该是noop。反例: // Naive attempt at resetting the object so it does nothing: let client = {}; // This will produce a type error: client.some.nested.

我想在生产代码中模拟一个JavaScript对象,这样它就可以配置为不做任何事情。对象非常复杂,具有许多属性,包括嵌套属性和函数。访问任何属性都不应导致TypeError,并且不应有任何副作用。运行任何函数名都应该是noop。反例:

// Naive attempt at resetting the object so it does nothing:
let client = {};

// This will produce a type error:
client.some.nested.prop.here = 10;

// This will produce a type error:
console.log(client.some.nested.prop.here);

// This will also produce a type error:
client.someFunction();
到目前为止,我尝试使用代理处理程序:

const clientHandler={
设置:()=>true,
获取:()=>{
返回新代理(()=>{},clientHandler);
},
有:()=>对,
应用:()=>{
log('Noop函数,改用模拟客户端');
},
};
让client=newproxy({},clientHandler);
//没有类型错误,代码继续工作。
client.some.nested.prop.here=10;
//没有类型错误,代码继续工作。
log(client.some.other.nested.prop);
//也没有类型错误,

client.does.not.exist.someFunction()说得很清楚,您想要模拟生产中的某些东西,以便在开发中使用?或者你想模仿一些将在生产中使用的东西?好问题。我想在产品中运行时模拟一些东西。如果我想在dev中模拟一些东西,我会使用已经在测试环境中使用过的许多工具中的一个?或者你想模仿一些将在生产中使用的东西?好问题。我想在产品中运行时模拟一些东西。如果我想在dev中模拟一些东西,我会使用已经在测试环境中使用的许多工具之一。