0

考虑以下 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
    }
}

我无法达到“做某事”。怎么了?

4

1 回答 1

3

好吧,仅仅因为你可能没有阅读评论,你有一个错字constructor(它说constractor)。

这有效:

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");
       }
   }
于 2013-06-24T04:23:42.380 回答