javascript - 如何让 console.log 输出 getter 结果而不是字符串 "[Getter/Setter]"?

标签 javascript node.js properties console getter-setter

在这段代码中:

function Cls() {
    this._id = 0;
    Object.defineProperty(this, 'id', {
        get: function() {
            return this._id;
        },
        set: function(id) {
            this._id = id;
        },
        enumerable: true
    });
};
var obj = new Cls();
obj.id = 123;
console.log(obj);
console.log(obj.id);

我想得到 { _id: 123, id: 123 } 但我得到 { _id: 123, id: [Getter/Setter] }

有没有办法让 console.log 函数使用 getter 值?

最佳答案

你可以使用 console.log(Object.assign({}, obj));

关于javascript - 如何让 console.log 输出 getter 结果而不是字符串 "[Getter/Setter]"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072671/

相关文章:

swift - 如何快速检查对象数组中是否存在属性值

javascript - 通过 Javascript 的强制 CSS 规则被部分应用

c# - 我可以在 Microsoft Visual Studio 2010 中缩小 JavaScript 或 aspx 代码吗

javascript - 是否可以使用单个 Node.js 服务器文件来提供 HTML 页面并处理来自该页面的 POST 请求?

JavaScript - 识别具有唯一属性的对象

c# - 我如何确保依赖属性(property)的其他人的属性(property)值(value)?

javascript - NWJS ('child_process' ).exec 命令未找到

javascript - ChartJS - 覆盖属性(AngularJS)

javascript - Node-config 全局配置

javascript - 我应该使用什么模式在 Node/JavaScript 中制作监听机器人?