0
変数に格納されたキーを使用してオブジェクト内でコールバック関数セットを取得しようとしています。私はノードv6.3.1を使用しています。 runCommand関数内でコールバック関数にアクセスしようとしたときしかし、それはundefinedを返しますJavascriptオブジェクトが変数キーで値を返さない
function ManageCommands() {
var scope = this;
scope.commands = {};
return {
addCommand: function (command, callback) {
scope.commands[command] = callback;
},
compileCommands: function() {
return Object.keys(scope.commands);
},
runCommand: function (key) {
console.log('command', key);
console.log('commands', scope.commands);
console.log(scope.commands[key]);
}
};
}
:
$ node server.js
command test me
commands { hello: [Function], 'test me': [Function] }
undefined
$ node server.js
command hello
commands { hello: [Function], 'test me': [Function] }
undefined
私は本当に簡単な何かが欠けてるよう、コマンドが文字列型で返される感じ。
console.log(scope.commands[key]);
は[関数]を返すと予想していますが、それは起こっていません。
洞察?
もっとコードを見る必要があります。 server.jsがManageCommandsオブジェクトをどのように呼び出すのですか?問題を再現するjsbinまたはjsfiddleを教えてもらえますか? –
'runCommand'では、' console.log( 'command'、JSON.stringify(key)) 'を実行し、取得したものを報告してください。 – trincot