简体   繁体   English

如何在运行时访问Typescript类的公共属性(调试)? 仅构造函数和函数可访问

[英]how can I access typescript class public property at run time (debugging)? only the constructor and functions are accessible

I am implementing a DI container for my framework in typescript, and want to know my class constructor parameters and properties for instantiating. 我正在用打字稿为我的框架实现DI容器,并且想知道我的类构造函数的参数和实例化属性。 Here is an example: 这是一个例子:

interface IDriver 
{
    Drive(): void
}

class DriverA implements IDriver 
{
    public Tickets: Array<Ticket>;
    public Name: String;

    public Drive() {
        //Driving...
    }
}

I am passing the interface name IDriver as string (because I was not able to pass the interface as a parameter) and concrete class DriverA to my registration routine. 我将接口名称IDriver传递为字符串(因为无法将接口作为参数传递)和具体的类DriverA传递给我的注册例程。 Latter in resolving state, to instantiate DriverA, I got the constructor and the Drive method, but I can't find the properties such as Tickets and Name. 在解析状态下,要实例化DriverA,我得到了构造函数和Drive方法,但找不到诸如Tickets和Name之类的属性。 How can I access those properties? 如何访问这些属性? is it possible? 可能吗?

Properties are only available if you initialize them eg: 属性只有在初始化它们后才可用,例如:

class DriverA 
{
    public Tickets = [];
    public Name = "";

    public Drive() {
        //Driving...
    }
}

will generate : 将产生:

var DriverA = (function () {
    function DriverA() {
        this.Tickets = [];
        this.Name = "";
    }
    DriverA.prototype.Drive = function () {
        //Driving...
    };
    return DriverA;
})();

Notice this.Tickets . 注意this.Tickets PS: they only get added after the constructor is called. PS:仅在调用构造函数后才添加它们。 ie new DriverA() new DriverA()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword? 如何通过构造函数的实例访问构造函数的属性? - How can I access a property of a constructor through an instance of that constructor? 打字稿:如何在构造函数之外初始化类属性 - Typescript: How to initialise a class property outside constructor 如何从JavaScript模块中的私有和公共函数访问公共财产? - How to access public property from private and public functions in a JavaScript module? Azure 函数(打字稿)- 如何运行设置/引导程序 function - Azure Functions (Typescript) - How can I run a setup/bootstrapper function 如何访问另一个类/ TypeScript 中的类 - How can I access a class inside another class / TypeScript 在 typescript 访问 class 属性 - Access class property in typescript 我可以将函数导入typescript类文件吗? - Can I import functions into a typescript class file? 打字稿:无法访问继承的类构造函数中的成员值 - Typescript: can not access member value in inherited class constructor 可以使用公共方法访问私有类的属性吗? - Can one access a private class property in a public method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM