如何找到我的对象被冻结的位置?

boy*_*ple 0 javascript react-native expo

我收到此错误:

Error: You attempted to set the key `TpDeF3wd6UoQ6BjEFmwz` with the value `{"seen":true}` on an object that is meant to be immutable and has been frozen.
Run Code Online (Sandbox Code Playgroud)

我如何发现哪些代码直接/间接冻结我的对象并使其不可变?

我已经通过完全重写逻辑解决了开发中的错误,但我想了解如何调试此类错误。

Kei*_*ith 5

一种想法是将其替换Object.freeze为您自己的记录堆栈,然后调用旧的冻结。

下面是一个例子,你可以看到它在30:8

此代码片段中的行号没有对齐,只是因为 SO 代码片段将添加一些额外的包装器代码,但在生产中这应该为您提供正确的行号。

'use strict';

function DebugFreeze() {
  const oldFree = Object.freeze;
  Object.freeze = (...args) => {
    console.log(new Error("Object Frozen").stack);
    return oldFree.call(Object, ...args);  
  }
}

DebugFreeze();


const a = { one: 1 };

a.two = 2;

Object.freeze(a);

a.three = 3;

console.log("here");
Run Code Online (Sandbox Code Playgroud)