在php中,我们可以这样做:
$classInstance = new MyClass();
$className = get_class_name($classInstance);
$newInstance = new $className;
如何在javascript中做同样的事情?
我尝试使用Object.creat
和Object.assign
,但是从新类中调用方法时,仍在引用旧类:
const newInstance = Object.assign(
Object.create(
Object.getPrototypeOf(oldInstace)
),
oldInstance
);
newInstance.method(); // "this" still referencing oldInstance