从类实例实例化新类

时间:2019-08-18 00:56:55

标签: javascript ecmascript-7

在php中,我们可以这样做:

$classInstance = new MyClass();
$className = get_class_name($classInstance);
$newInstance = new $className; 

如何在javascript中做同样的事情?

我尝试使用Object.creatObject.assign,但是从新类中调用方法时,仍在引用旧类:

  const newInstance = Object.assign(
    Object.create(
      Object.getPrototypeOf(oldInstace)
    ),
    oldInstance
  );

  newInstance.method(); // "this" still referencing oldInstance

0 个答案:

没有答案