Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking sense of class extension and instance properties in Coffee Script
    primarykey
    data
    text
    <p>I've just gotten into Coffee Script and am having issues figuring out how class extension works. It seems that properties defined as instance properties on a parent class are being implemented as static properties when that class is extended.</p> <p>I have a class, called Foo, which I want to use as a base class for two sub classes, Bar and Goo. I give Foo an instance property called foobs and a method for adding a foob, like so: </p> <pre class="lang-coffee-script prettyprint-override"><code>class Foo foobs:[] addFoob: (foob) -&gt; @foobs.push(foob) </code></pre> <p>And I extend Foo with Bar and Goo, and create new instances, like so:</p> <pre class="lang-coffee-script prettyprint-override"><code>class Bar extends Foo otherMethod: -&gt; alert 'doing other stuff' class Goo extends Foo secondMethod: -&gt; alert 'doing second stuff' barInstance = new Bar() gooInstance = new Goo() </code></pre> <p>But then when I add a Foob to barInstance, it also gets added to gooInstance!</p> <pre class="lang-coffee-script prettyprint-override"><code>barInstance.addFoob('test') console.log gooInstance.foobs (outputs ["test"]) </code></pre> <p>Clearly I'm doing something wrong here. I want barInstance and gooInstance to each have their own "foobs" property, but for some reason it seems that even though foobs is an instance property on Foo, it's getting assigned as a class property on Bar and Goo. Any ideas on how to get around this? Or maybe there's different syntax I'm unaware of?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload