{"id":892,"date":"2022-08-30T15:08:55","date_gmt":"2022-08-30T15:08:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/coffeescript-class-inheritance-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:55","modified_gmt":"2022-08-30T15:08:55","slug":"coffeescript-class-inheritance-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/coffeescript-class-inheritance-collection-of-common-programming-errors\/","title":{"rendered":"CoffeeScript class inheritance-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to figure out how inheritance works in coffeescript. Here&#8217;s a simplified example of my code:<\/p>\n<pre><code>class Parent\n\n  constructor: (attrs) -&gt;\n    for own name,value of attrs\n      this[name] = value\n\nParent.from_json_array = (json, callback) -&gt;\n  for item in JSON.parse(json)\n    obj = new ChildA item  # [1]\n    callback obj\n\nclass ChildA extends Parent\n\nclass ChildB extends Parent\n\nChildA.from_json_array(\"[{foo: 1}, {foo: 2}]\") (obj) -&gt;\n  console.log obj.foo\n<\/code><\/pre>\n<p>What do I need to put on the line marked <code>[1]<\/code> to use the correct child class here? This works, but only creates objects with a prototype of <code>ChildA<\/code>. I&#8217;ve tried something like:<\/p>\n<pre><code>Parent.from_json_array = (json, callback) -&gt;\n  klass = this.prototype\n  for item in JSON.parse(json)\n    obj = klass.constructor item  # [1]\n    callback obj\n<\/code><\/pre>\n<p>&#8230; but this leaves <code>obj<\/code> as undefined in my callback function (TypeError: Cannot read property &#8216;foo&#8217; of undefined&#8221;.<\/p>\n<p>Whats the magic incantation in CoffeeScript to be able to create a new object of a class, where the class is variable?<\/p>\n<ol>\n<li>\n<p>Nevermind, I figured it out:<\/p>\n<pre><code>Parent.from_json_array = (json, callback) -&gt;\n  klass = this\n  for item in JSON.parse(json)\n    obj = new klass item\n    callback obj\n<\/code><\/pre>\n<p>Turns out you can just <code>new<\/code> a class stored in a variable. I thought I had tried this before, but was getting a syntax error.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:55:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to figure out how inheritance works in coffeescript. Here&#8217;s a simplified example of my code: class Parent constructor: (attrs) -&gt; for own name,value of attrs this[name] = value Parent.from_json_array = (json, callback) -&gt; for item in JSON.parse(json) obj = new ChildA item # [1] callback obj class ChildA extends Parent class ChildB extends [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-892","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}