【问题标题】:typescript - How can i get object class instance? [closed]typescript - 我怎样才能获得对象类实例? [关闭]
【发布时间】:2013-06-17 17:10:45
【问题描述】:

我有一个类 classA 的对象,它继承自类 classB

我的问题是:我如何知道我的对象是否继承自 classB

我试过了: B 类的对象实例

但它不起作用!

当我将 instanceof 添加到任何 ts 文件时 我得到了

0x800a138f - Microsoft JScript 运行时错误:无法获取属性“原型”的值:对象为空或未定义

var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype; //exception occurs in this line
    d.prototype = new __();
};

【问题讨论】:

  • 您应该尝试发布您实际使用的代码。
  • 这不是太本地化。使用更好的示例代码可以更好地表达这个问题,但这并不会使实际问题过于具体。

标签: javascript inheritance typescript


【解决方案1】:

此错误的一个原因是如果您派生的类来自文件中的派生类之后

这个(破坏 Ryan 的回答):

class B extends A {
}

class A {
}

var x = new B();
console.log(x instanceof A); // Error

... 导致您在Playground 中描述的错误。

我认为在 codeplex 上已经有一个工作项,但是已经晚了,我还没有检查...

【讨论】:

    【解决方案2】:

    这通常有效:

    class A {
    }
    
    class B extends A {
    }
    
    var x = new B();
    console.log(x instanceof A); // true
    

    【讨论】:

    • 当我将“instanceof”添加到 ts 文件时,我得到 0x800a138f - Microsoft JScript 运行时错误:无法获取属性“原型”的值:对象为空或未定义
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-08-03
    • 2020-12-06
    • 2012-11-20
    • 2011-06-04
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多