在这段代码中:
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/