Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are defining a constructor but expecting that it behaves like a function. The constructor, however, just sets values in the object to be returned. Since your constructor does not set any attributes in the initializing object, it really does not useful.</p> <p>You have some alternatives:</p> <ol> <li><p>Initialize the class as @amaud sugested.</p></li> <li><p>Returns the value from the constructor as @amaud sugested (which does not make much sense to me. This is not the function of a constructor as I feel it. In this case the solution #3 seems better).</p></li> <li><p>define a function instead of a class. IMHO, is the most simple and functional solution</p> <pre><code>@Point = (x, y) -&gt; if Float32Array? then Float32Array([x,y]) else Array(x,y) </code></pre></li> <li><p>If you want <code>Point</code> to be either a specialization of <code>Float32Array</code> or <code>Array</code>, use the option #1 but make <code>Point</code> to inherit from the class you want:</p> <pre><code>superclass = if Float32Array? then Float32Array else Array class @Point extends superclass constructor: (x,y) -&gt; @[0] = x @[1] = y </code></pre></li> </ol> <p><strong>EDIT</strong>: @amaud676875 posted an interesting question as a comment. Since a reasonable answer would involve some code, I am posting the answer as a edit.</p> <p>@amaud, for verifying your point, I wrote the following CoffeeScript module:</p> <pre><code>class Float32Array extends Array first: -&gt; # Just for testing @[0] superclass = if Float32Array? then Float32Array else Array class @Point extends superclass constructor: (x,y) -&gt; @[0] = x @[1] = y </code></pre> <p>Then I imported the module in the console:</p> <pre><code>coffee&gt; point = require './point' { Point: { [Function: Point] __super__: [ constructor: [Object], first: [Function] ] }, Float32Array: { [Function: Float32Array] __super__: [] } } </code></pre> <p>and created a <code>Point</code>:</p> <pre><code> coffee&gt; p = new point.Point 3, 2 [ 3, 2 ] </code></pre> <p>This <code>Point</code> has the <code>first()</code> method from <code>Float32Array</code>:</p> <pre><code> coffee&gt; p.first() 3 </code></pre> <p>and <code>instanceof</code> says it is an instance of <code>Float32Array</code>, too:</p> <pre><code>coffee&gt; p instanceof point.Float32Array true </code></pre> <p>So I bet <code>new Point x, y</code> returns an instance of <code>Float32Array</code>. Of course it is an instance of <code>Point</code>, too, and it is not a problem because <code>Point</code> <em>is-a</em> <code>Float32Array</code>, to use a classical OOP expression.</p>
    singulars
    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.
    1. VO
      singulars
      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