如何从现有对象创建新对象';javascript中的s构造函数

如何从现有对象创建新对象';javascript中的s构造函数,javascript,Javascript,考虑以下JavaScript代码: if (oo instanceof MyType) { var newObj = new oo.constructor; // suppose the following check should be ok but it is not // newObj should have same constructor as its original oo. if (newObj instanceof MyType) {

考虑以下JavaScript代码:

if (oo instanceof MyType) {
    var newObj = new oo.constructor;

    // suppose the following check should be ok but it is not
    // newObj should have same constructor as its original oo.

    if (newObj instanceof MyType) {
        // do something
    }
}

我无法达到“做点什么”。怎么了?

嗯,仅仅因为您可能没有阅读注释,您在
构造函数
中有一个打字错误(上面写着
构造函数

这项工作:

function MyType() {}
oo = new MyType();

if(oo instanceof MyType)
   {
       var newObj = new oo.constructor(); // The mistake was in this line

       if( newObj instanceof MyType)
       {
           console.log("a contractor was killed by a constrictor while constructing");
       }
   }

嗯,仅仅因为您可能没有阅读注释,您在
constructor
中有一个打字错误(上面写着
constructor

这项工作:

function MyType() {}
oo = new MyType();

if(oo instanceof MyType)
   {
       var newObj = new oo.constructor(); // The mistake was in this line

       if( newObj instanceof MyType)
       {
           console.log("a contractor was killed by a constrictor while constructing");
       }
   }

oo.constr a constrUctor()?我将
constrUctor
更改为
constrUctor
,这没关系。我想他指的是constrUctor…与其问有什么问题,不如熟悉一下浏览器开发工具。在Chrome中加载页面并打开开发者工具(通常为F12或Ctrl+Shift+I,具体取决于您的操作系统)。现在,重新加载页面或执行任何需要的操作以访问相关代码。它将在“开发人员工具”窗口中显示错误。这是.oo.constr a ctor()?我将
constrUctor
改为
constrUctor
,这没关系。我想他指的是constrUctor…与其问有什么问题,不如熟悉一下浏览器开发工具。在Chrome中加载页面并打开开发者工具(通常为F12或Ctrl+Shift+I,具体取决于您的操作系统)。现在,重新加载页面或执行任何需要的操作以访问相关代码。它将在“开发人员工具”窗口中显示错误。这里是。打字错误不存在发布问题编辑:Stypo不存在发布问题编辑:S