Getter is an accessor function for a property that returns the value of the property. Here's what an object with a getter looks like:
var obj = {
get example() {
console.log("getter was called");
return Math.floor(Math.random() * 100);
}
};
console.log(obj.example);
Run codePlease note that when we read the value of a property example, the function starts, even if it does not look like a function call.
What this part of the MDD documents says is that Object.assignthis getter will call, it will not create an equivalent getter on the target. So:
var obj = {
get example() {
console.log("getter was called");
return Math.floor(Math.random() * 100);
}
};
var obj2 = Object.assign({}, obj);
console.log(obj2.example);
console.log(obj2.example);
console.log(obj2.example);
Run codeobj example getter, obj2 example - value. Object.assign , ot obj2.example.
, Object.assign:
function copyProperties(target, source) {
Object.getOwnPropertyNames(source).forEach(name => {
Object.defineProperty(
target,
name,
Object.getOwnPropertyDescriptor(source, name)
);
});
return target;
}
var obj = {
get example() {
console.log("getter was called");
return Math.floor(Math.random() * 100);
}
};
var obj2 = copyProperties({}, obj);
console.log(obj2.example);
console.log(obj2.example);
console.log(obj2.example);
, (, example getter obj), .