Pebble Coding

プログラマーの作業メモ

JavaScript オブジェクトのメソッド名を連想配列を使い文字列を使って呼び出す

var sports = 
    {   
        swim : function( arg1, arg2 ){
           print( "swim " + arg1 + " km " +  arg2 );
        },
        walk : function( arg1, arg2 ){
           print( "walk " + arg1 + " km " + arg2 );
       }
    };

sports['swim']( 1, "feel good!" );          // swim 1 km feel good!
sports['walk']( 42, "tired!" );                // walk 42 km tired!
sports.swim( 100, "can't believe!" );   // swim 100 km can't believe!
sports.walk( 1000, "crazy!" );            // walk 1000 km crazy!