{"id":1396,"date":"2013-07-18T11:55:00","date_gmt":"2013-07-18T11:55:00","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1396"},"modified":"2013-07-18T11:55:00","modified_gmt":"2013-07-18T11:55:00","slug":"self-modifying-javascript-objects","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/self-modifying-javascript-objects\/","title":{"rendered":"Self-modifying Javascript objects"},"content":{"rendered":"<p>I thought it&#8217;d be interesting to consider Javascript objects which can modify their own behavior over time. A use case for this (what I&#8217;m doing) is pages of data, where data is first looked up, then cached in memory, then cleared. A simple case (demonstrated below) is memoization.<\/p>\n<p>The following query will do an ajax lookup of some data:<\/p>\n<pre lang=\"Javascript\">\nfunction loadData() {\n  var data;\n\n  $.ajax({\n    url: \"blocks\/\" + this.block,\n    async: false\n  }).done(function(blockData) {\n    data = blockData.split(\"\\n\")\n    for (var i = 0; i < data.length; i++) {\n      data[i] = data[i].split(\"\\t\")\n    }\n  });\n\n  return data;\n}\n\nvar x = {block: 'xabnh', getData: loadData}\n\nx.getData();\nArray[2501]\n<\/pre>\n<p>This shows how one might trigger caching the results. Once retrieved, the cached data is hidden within a closure, and the mechanism for retrieving data is updated. In the case of a memory manager, these pages are later cleared out by replacing the memoization function with a third function, which starts the process all over again.<\/p>\n<pre lang=\"Javascript\">\nvar x = {block: 'xabnh', getData: memoize(loadData)}\n\nfunction memoize(getData) {\n  return function() {\n    console.log('loading');\n    var data = getData.apply(this);\n    function savedData() {\n      console.log('memoized');\n      return data;\n    }\n    this.getData = savedData;\n  \n    return data;\n  }\n}\n\nx.getData()\nloading\nArray[2501]\nx.getData()\nmemoized\nArray[2501]\n<\/pre>\n<p>There are a few downsides to this approach; data in the closure is hidden from inspection, the current state of the object is hidden, and can change quickly. A generalized approach to this problem might pass an object into the getData function to show state or trigger a debug mode.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought it&#8217;d be interesting to consider Javascript objects which can modify their own behavior over time. A use case for this (what I&#8217;m doing) is pages of data, where data is first looked up, then cached in memory, then cleared. A simple case (demonstrated below) is memoization. The following query will do an ajax &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/self-modifying-javascript-objects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Self-modifying Javascript objects&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,29],"tags":[168,302],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1396"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1396"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1396\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}