日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

gettype获取类名_在TypeScript中运行时获取对象的类名

發布時間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gettype获取类名_在TypeScript中运行时获取对象的类名 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在TypeScript中運行時獲取對象的類名

是否可以使用typescript在運行時獲取對象的類/類型名稱?

class MyClass{}

var instance = new MyClass();

console.log(instance.????); // Should output "MyClass"

Adam Mills asked 2019-04-11T03:14:30Z

9個解決方案

306 votes

簡單回答:

class MyClass {}

const instance = new MyClass();

console.log(instance.constructor.name); // MyClass

console.log(MyClass.name); // MyClass

但是:請注意,使用縮小代碼時名稱可能會有所不同。

Mikael Couzic answered 2019-04-11T03:14:50Z

21 votes

我知道我遲到了,但我發現這也有效。

var constructorString: string = this.constructor.toString();

var className: string = constructorString.match(/\w+/g)[1];

另外...

var className: string = this.constructor.toString().match(/\w+/g)[1];

上面的代碼將整個構造函數代碼作為字符串獲取并應用正則表達式來獲取所有“單詞”。 第一個單詞應該是'function',第二個單詞應該是該類的名稱。

希望這可以幫助。

answered 2019-04-11T03:15:27Z

19 votes

看到這個問題。

由于TypeScript被編譯為JavaScript,因此在運行時您運行的是JavaScript,因此將應用相同的規則。

Matt Burland answered 2019-04-11T03:16:01Z

15 votes

我的解決方案不是依賴類名。 object.constructor.name在理論上有效。 但是如果你在像Ionic這樣的東西上使用TypeScript,那么一旦你進入制作階段,它就會起火,因為Ionic的制作模式會縮小Javascript代碼。 因此,類被命名為“a”和“e”。

我最終做的是在構造函數為其分配類名的所有對象中都有一個typeName類。 所以:

export class Person {

id: number;

name: string;

typeName: string;

constructor() {

typeName = "Person";

}

是的,這不是問的,真的。 但是使用constructor.name來解決可能會縮小規模的問題只是讓人頭疼。

SonOfALink answered 2019-04-11T03:16:42Z

8 votes

您需要先將實例強制轉換為any,因為Function的類型定義沒有name屬性。

class MyClass {

getName() {

return (this).constructor.name;

// OR return (this as any).constructor.name;

}

}

// From outside the class:

var className = (new MyClass()).constructor.name;

// OR var className = (new MyClass() as any).constructor.name;

console.log(className); // Should output "MyClass"

// From inside the class:

var instance = new MyClass();

console.log(instance.getName()); // Should output "MyClass"

更新:

使用TypeScript 2.4(可能更早),代碼可以更清晰:

class MyClass {

getName() {

return this.constructor.name;

}

}

// From outside the class:

var className = (new MyClass).constructor.name;

console.log(className); // Should output "MyClass"

// From inside the class:

var instance = new MyClass();

console.log(instance.getName()); // Should output "MyClass"

Westy92 answered 2019-04-11T03:17:20Z

5 votes

在Angular2中,這有助于獲取組件名稱:

getName() {

let comp:any = this.constructor;

return comp.name;

}

comp:any是必需的,因為typescript compile會發出錯誤,因為Function最初沒有屬性名。

Admir Sabanovic answered 2019-04-11T03:17:54Z

3 votes

不得不添加“.prototype”。 使用:error TS2339: Property 'name' does not exist on type 'Function'。

否則使用以下代碼:error TS2339: Property 'name' does not exist on type 'Function',我有打字稿錯誤:

error TS2339: Property 'name' does not exist on type 'Function'。

Flox answered 2019-04-11T03:18:34Z

3 votes

完整的TypeScript代碼

public getClassName() {

var funcNameRegex = /function (.{1,})\(/;

var results = (funcNameRegex).exec(this["constructor"].toString());

return (results && results.length > 1) ? results[1] : "";

}

Nati Krisi answered 2019-04-11T03:19:05Z

0 votes

如果您已經知道期望的類型(例如,當方法返回聯合類型時),那么您可以使用類型保護。

例如,對于原始類型,您可以使用一種類型的保護:

if (typeof thing === "number") {

// Do stuff

}

對于復雜類型,您可以使用instanceof guard:

if (thing instanceof Array) {

// Do stuff

}

Cocowalla answered 2019-04-11T03:19:43Z

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的gettype获取类名_在TypeScript中运行时获取对象的类名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。