{"id":9,"date":"2014-05-27T22:36:47","date_gmt":"2014-05-27T22:36:47","guid":{"rendered":"http:\/\/localhost\/wordpress\/?p=9"},"modified":"2014-05-29T13:23:35","modified_gmt":"2014-05-29T13:23:35","slug":"instead-of-conditional-call","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=9","title":{"rendered":"instead of conditional call"},"content":{"rendered":"<p>I want to create an easy adjustable command processor class with some implemented callable commands.<br \/>\nThe command definition like this:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n...\r\nmyClass.prototype.commandFirst = function(uID){\r\n  ... do something ...\r\n  return true;\r\n};\r\nmyClass.prototype.commandSecond = function(uID){\r\n  ... do something ...\r\n  return true;\r\n};\r\n...\r\n<\/pre>\n<p>In a conditional world we can do the following:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nmyClass.prototype.commandReceived = function(cmd){\r\n  var x = cmd.command, uID = x.u, vCmd = x.cmd, ret = false;\r\n  \r\n  \/\/ simple conditional way\r\n  if (vCmd == 'commandFirst'){ ret = this.commandFirst(uID); }\r\n  else if (vCmd == 'commandSecond '){ ret = this.commandSecond(uID); }\r\n  \/\/ !!! of course we need to edit this section all time\r\n  \/\/ !!! when we want to extend the class with a new command \r\n  else { console.log('unimplemented command: ' + vCmd); }\r\n\r\n  return ret;\r\n};\r\n<\/pre>\n<p>Be careful we need to edit this statement every time when we want to put new command into the collection.<br \/>\nSo we can avoid this if we can release the old method&#8230;<br \/>\nInstead of this long way use the tricky way based on the javascript soul:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">myClass.prototype.commandReceived = function(cmd){\r\n  var x = cmd.command, uID = x.u, vCmd = x.cmd, ret = false;\r\n\r\n  \/\/ Tricky call here.\r\n  this[vCmd] ? (ret = this[vCmd](uID)) : console.log('unimplemented: ' + vCmd);\r\n\r\n  return ret;\r\n};\r\n<\/pre>\n<p>This is because<\/p>\n<p><code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">myClass.prototype.commandSecond<\/code> callable as <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">myClass['commandSecond']<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to create an easy adjustable command processor class with some implemented callable commands. The command definition like this: In a conditional world we can do the following: Be careful we need to edit this statement every time when we want to put new command into the collection. So we can avoid this if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9"}],"version-history":[{"count":2,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}