在这
playground example
我正在尝试将对象文本转换为具有属性的对象。这不符合预期。
class X {
y: string;
get hi(): string {
return `hi ${this.y}`;
}
}
const a = new X();
a.y = 'bob';
const b = { y: 'ham' } as X;
const c = Object.assign(new X(), b);
document.write(a.hi); // ouputs "hi bob"
document.write("<br>");
document.write((b.hi === undefined).toString()); // outputs "true"
document.write("<br>");
Object.assign(b, X.prototype);
document.write((b.hi !== undefined).toString()); // outputs "true"
document.write("<br>");
document.write(b.hi); // **outputs "hi defined" -- want "hi ham"**
document.write("<br>");
document.write(c.hi); // outputs "hi ham"
document.write("<br>");
演员中有什么我不知道的东西可以让这个工作,还是我应该
Object.assign
就像我在做
const c = Object.assign(new X(), { y: 'ham' });
?