javascript - 从类实例实例化新类
问题描述
在 php 中我们可以这样做:
$classInstance = new MyClass();
$className = get_class_name($classInstance);
$newInstance = new $className;
我怎样才能在javascript中做同样的事情?
我尝试使用Object.creat
and Object.assign
,但是当从新类调用方法时,旧的仍然被引用:
const newInstance = Object.assign(
Object.create(
Object.getPrototypeOf(oldInstace)
),
oldInstance
);
newInstance.method(); // "this" still referencing oldInstance