gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:05:28 26 4
gpt4 key购买 nike

在这段代码中:

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/

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