0
我將Foo
實例寫入JSON並恢復回來。然後,我嘗試使用實例的方法,但我得到的錯誤:如何正確地將_foo投射到`Foo`類型?
class Foo{
\t constructor(name,surname){
\t \t this.name=name;
this.surname=surname;
};
fullName(){
\t return this.name + ' ' + this.surname;
};
};
let foo = new Foo('John', 'Smith');
console.log(foo.fullName());
let json = JSON.stringify(foo);
let _foo = JSON.parse(json);
Object.setPrototypeOf(_foo, Object.getPrototypeOf(Foo));
// Uncaught TypeError: _foo.fullName is not a function
console.log(_foo.fullName());
如何正確給_foo轉換爲Foo
類型?