javascript - Object.defineProperty 覆盖只读属性

标签 javascript node.js properties ecmascript-6 prototype

在我们的 NodeJS 应用程序中,我们通过扩展默认的错误对象来定义自定义错误类:

"use strict";
const util = require("util");

function CustomError(message) {
    Error.call(this);
    Error.captureStackTrace(this, CustomError);
    this.message = message;
}

util.inherits(CustomError, Error);

这允许我们抛出 CustomError("Something"); 并正确显示堆栈跟踪,instanceof Errorinstanceof CustomError 正常工作。

但是,为了在我们的 API 中返回错误(通过 HTTP),我们希望将错误转换为 JSON。在出现错误时调用 JSON.stringify() 会导致 “{}”,这对于消费者来说显然不是真正的描述性内容。

为了解决这个问题,我考虑重写 CustomError.prototype.toJSON(),以返回一个包含错误名称和消息的对象字面量。 JSON.stringify() 将把这个对象字符串化,一切都会很好:

// after util.inherits call

CustomError.prototype.toJSON = () => ({
    name    : "CustomError",
    message : this.message
});

但是,我很快发现这会引发 TypeError: Cannot assign to read only property 'toJSON' of Error。这可能是有道理的,因为我正在尝试写入原型(prototype)。所以我改为更改构造函数:

function CustomError(message) {
    Error.call(this);
    Error.captureStackTrace(this, CustomError);
    this.message = message;
    this.toJSON = () => ({
        name    : "CustomError",
        message : this.message
    });
}

这样(我预计),将使用 CustomError.toJSON 函数并忽略 CustomError.prototype.toJSON(来自 Error)。

不幸的是,这只会在对象构造时抛出错误:无法分配给 CustomError 的只读属性“toJSON”

接下来,我尝试从文件中删除 "use strict";,这某种程度上解决了问题,因为不再抛出任何错误,尽管 JSON.stringify() 根本没有使用 toJSON() 函数。

在这一点上,我只是绝望,只是尝试随机的事情。最终我最终使用 Object.defineProperty() 而不是直接分配给 this.toJSON:

function CustomError(message) {
    Error.call(this);
    Error.captureStackTrace(this, CustomError);
    this.message = message;
    Object.defineProperty(this, "toJSON", {
        value: () => ({
            name    : "CustomError",
            message : this.message
        })
    });

这非常有效。在严格模式下,没有错误被调用,并且 JSON.stringify() 返回 {"name:"CustomError", "message": "Something"} 就像我想要的

所以尽管它现在可以正常工作,但我仍然想知道:

  1. 为什么这行得通?我希望它等同于 this.toJSON = ... 但显然不是。
  2. 它应该像这样工作吗? IE。依赖这种行为是否安全?
  3. 如果不是,我该如何正确重写 toJSON 方法? (如果可能的话)

最佳答案

Why does this work exactly?

Object.defineProperty 只是定义一个属性(或者改变它的属性,如果它已经存在并且是可配置的)。与赋值 this.toJSON = … 不同,它不关心任何继承,也不检查是否存在可能是 setter 或不可写的继承属性。

Should it work like this? I.e. is it safe to depend on this behaviour?

是的,是的。也许您甚至可以在原型(prototype)上使用它。


对于您的实际用例,给定最新的 node.js 版本,使用 extends Error 的 ES6 类以获得最佳结果。

关于javascript - Object.defineProperty 覆盖只读属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821837/

相关文章:

javascript - 以 UTF-8 字符串存储二进制数据

node.js - 在 Facebook `og:image` 标签中使用 Google Cloud Storage 的公共(public) URL

mysql - NodeJS 转换 MYSQL 日期

javascript - 调用渲染作为回调时访问 View 属性

java - 如何 Autowiring Spring Environment 对象

javascript - react 原生 + react 原生路由器通量 : Why is background color white during transition?

javascript - 使用 jQuery/Javascript 在将鼠标悬停在文本上时切换 div 中的图像

node.js - NodeJs/Electron : How to wait for function to finish before ipcMain send back his answer?

Java 8 : Observable List - Invalidation Listener nor Change Listener is called in case of property change

javascript - 用javascript替换图像上的src