DeprecationReportBody

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

注意:此功能在 Web Workers 中可用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

Reporting API 中的 DeprecationReportBody 接口代表弃用报告的正文。

当被 ReportingObserver 观察的文档中使用了已弃用的功能(例如已弃用的 API 方法)时,会生成弃用报告。除了支持此 API 外,接收有用的弃用警告还依赖于浏览器供应商为已弃用的功能添加这些警告。

构造函数

Report.Typedeprecation 时,DeprecationReportBody 的实例将作为 Report.body 的值返回。该接口没有构造函数。

实例属性

此接口还继承了 ReportBody 的属性。

DeprecationReportBody.id Experimental

一个字符串,表示已弃用的功能或 API,例如 NavigatorGetUserMedia。这可用于按已弃用的功能对报告进行分组。

DeprecationReportBody.anticipatedRemoval Experimental

一个 Date 对象(显示为字符串),表示该功能预计从当前浏览器中移除的日期。如果日期未知,此属性将返回 null

DeprecationReportBody.message Experimental

一个字符串,包含对弃用的可读描述,包括信息(例如,是否有较新的功能已取代它)。这通常与浏览器在其开发者工具控制台中显示已弃用功能使用时的消息相匹配(如果可用)。

DeprecationReportBody.sourceFile Experimental

一个字符串,包含使用已弃用功能所在的源文件的路径(如果已知),否则为 null

DeprecationReportBody.lineNumber Experimental

一个数字,表示使用已弃用功能所在的源文件的行号(如果已知),否则为 null

DeprecationReportBody.columnNumber Experimental

一个数字,表示使用已弃用功能所在的源文件的列号(如果已知),否则为 null

实例方法

此接口还继承了 ReportBody 的方法。

DeprecationReportBody.toJSON() Experimental

一个序列化器,它返回 InterventionReportBody 对象的 JSON 表示形式。

示例

在我们 deprecation_report.html 示例中,我们创建了一个简单的报告观察者来观察我们网页上已弃用功能的使用情况。

js
const options = {
  types: ["deprecation"],
  buffered: true,
};

const observer = new ReportingObserver((reports, observer) => {
  reportBtn.onclick = () => displayReports(reports);
}, options);

然后,我们告诉它使用 ReportingObserver.observe() 开始观察报告;这告诉观察者开始在其报告队列中收集报告,并运行构造函数中指定的函数。

js
observer.observe();

由于我们在 ReportingObserver() 构造函数中设置了事件处理程序,现在我们可以单击按钮来显示报告详细信息。

image of a jolly bearded man with various stats displayed below it about a deprecated feature

报告详细信息通过 displayReports() 函数显示,该函数将观察者回调的 reports 参数作为其参数。

js
function displayReports(reports) {
  const outputElem = document.querySelector(".output");
  const list = document.createElement("ul");
  outputElem.appendChild(list);

  reports.forEach((report, i) => {
    const listItem = document.createElement("li");
    const textNode = document.createTextNode(
      `Report ${i + 1}, type: ${report.type}`,
    );
    listItem.appendChild(textNode);
    const innerList = document.createElement("ul");
    listItem.appendChild(innerList);
    list.appendChild(listItem);

    for (const [key, value] of Object.entries(report.body)) {
      const innerListItem = document.createElement("li");
      innerListItem.textContent = `${key}: ${value}`;
      innerList.appendChild(innerListItem);
    }
  });
}

reports 参数包含观察者报告队列中所有报告的数组。我们使用基本的 for 循环遍历每个报告,然后使用 for...in 结构遍历报告正文(DeprecationReportBody 实例)中的每个条目,在列表项中显示每个键/值对。

规范

规范
弃用报告
# deprecationreportbody

浏览器兼容性

另见