変数を特定の型に明示的にキャストする一般的な方法はありますか?たとえば :jsの明示的な型キャスト
cast
がその魔法の鋳造法であると考えられる
var b = true;
var str = "";
var n = 5;
cast(someVariable, typeof b); //someVariable become a boolean
cast(someVariable, typeof str); //someVariable become a string
cast(someVariable, typeof n); //someVariable become an integer
。
可能なすべてのタイプをswitch
で列挙できることは明らかです。しかし、それを行うための[美しい]ネイティブな方法はありますか?
あなたは 'ブール(someVariable)のようなものを使用することはできません、番号(someVariableを);'? –
@MohitBhardwaj私はできますが、私は常に第二引数の型を知っているわけではありません。 –