これは、我々はそのインデックスによって配列を取得することができます方法です。配列インデックスのようなTypeScriptメソッドを呼び出すことは可能ですか?
var theArray=[];
theArray['something']=="Hello!";
console.log(theArray['something']); //Print "Hello!"
は今、同じことを想像し、私は機能を持っていると私はそれを呼び出すつもりです。このように:
export class myClass {
public callByIndex(theIndex:string) {
//this is how it works:
this.doSomething()
//And these are the ways I need to call them:
this.theIndex //of course error!
//or
this[theIndex]; //Another Error! How to do it?
}
private doSomething(){
console.log("Yesss, I called!");
}
}
let callTest = new myClass();
callTest.callByIndex("doSomething"); //Here I give the method name!
どうすれば可能ですか?
インデックスは何ですか?指定されたインデックスに基づいて値を取得するには、オブジェクト/配列が必要です。 'this.someArray [theIndex]' – Oen44
@ Oen44私はあなたが何を意味しているのか知っているので、私はJavaScriptのオブジェクト/配列について知っているので、私は**のように "配列インデックスと同じように" **、それらのインデックスで配列にアクセスする方法も同様です。 – M98