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中运行时获取对象的类名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 岗位po是什么意思_面试时,面试官问你有
- 下一篇: javascript中数组、冒泡排序、函