Reputation: 5040
I have a Class called Myclass
class Myclass {
constructor(
public title: string,
) { }
}
in the next example, I want to change the result of the spread operation
let myobject = new Myclass('hello');
console.log({...myobject});
result wanted for example
{
new_title_name : 'hello'
}
Upvotes: 1
Views: 844
Reputation:
This cannot be done. The ECMA-262 specification describes only one way a spread operator can work with objects, with no ability to override it.
If you want to change the set of key-value pairs spread out, you need to provide a different object. Such an object can be generated by a function, method or a property:
class Myclass {
get data() {
const result = {};
for (const k in this) {
if (typeof this[k] === 'number')
result[k.toUpperCase()] = this[k];
}
return result;
}
};
const obj = new Myclass();
obj.a = [];
obj.b = null;
obj.c = 13;
obj.d = 'test';
console.info({ ...obj.data });
Upvotes: 2