gpt4 book ai didi

javascript - 将整个窗口对象转换为字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:45 25 4
gpt4 key购买 nike

我需要检查整个窗口对象,以检查实际保存数据的位置。

如果我执行 window.toString() 我会得到 [object Window]"我还尝试使用 JSON.stringify(window) 函数并得到以下错误:

VM18766:1 Uncaught TypeError: Converting circular structure to JSON

有什么方法可以获得包括原型(prototype)函数在内的整个 javascript 对象内容?

我需要这个以便我可以在对象文本中搜索保存在对象中的特定内容以及该对象所在的位置。

最佳答案

如果您尝试执行 JSON.stringify(window),您将收到以下错误:

Uncaught TypeError: Converting circular structure to JSON

从这里this StackOverflow 帖子,您可以执行以下操作:

const p = document.getElementById('p');

const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};

p.innerHTML = JSON.stringify(window, getCircularReplacer());
<html>
<head>
</head>
<body>
<p id="p"></p>
<script src="app.js"></script>
</body>
</html>

关于javascript - 将整个窗口对象转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com