考虑以下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
}
}
我无法达到“做某事”。怎么了?
答案 0 :(得分: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");
}
}