Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to define an object on the prototype based on the super class value in Coffeescript?
    text
    copied!<h2>Question</h2> <p>Let's say I have the following class in CS</p> <pre><code>class Foo foo: one: 1 two: 2 </code></pre> <p>I'd like to have a <code>class Bar extends Foo</code> whose <code>foo</code> property returned <code>{one: 1, two: 2, three: 3}</code>. Is there a way I can do this in the class definition for <code>Bar</code> where I am only appending <code>three: 3</code> to the already existing <code>foo</code> property on the superclass <code>Foo</code>?</p> <h2>Use case / work around</h2> <p>I'm curious if it is possible to do something like I've explained above. However, due to my use case it is not a blocking issue since I can use Coffeescript's <code>super</code> call to work around it by making it a function.</p> <p>I'm currently using Backbone and I have two classes. One inherits from <code>Backbone.Model</code> and the other inherits from the first class. In the first class I'm setting the <code>defaults</code> property so that when this model is created, it has instance variables set if they're not passed in. The class inheriting from my first class has one additional key value pair to add to this <code>defaults</code> object though it would be the same situation if I wanted to overwrite a default.</p> <p>The object's default values in Backbone are obtained by using Underscore's <a href="http://underscorejs.org/#result"><code>result</code></a> method so the quick work around in this case is to simply make <code>defaults</code> a function that returns the same object. In Coffeescript this is incredibly easy and becomes:</p> <pre><code>class Foo foo: -&gt; one: 1 two: 2 </code></pre> <p>And then in <code>Bar</code> you can do the following:</p> <pre><code>class Bar extends Foo foo: -&gt; _.extends super, three: 3 </code></pre>
 

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