Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a trick that will fulfill the requirement most of the time:</p> <pre><code>msg = "Result-----------\n" class Dog #public variable dogName: null #private variables _private = [] constructor: (@dogName, surname) -&gt; _private[@dogName] = {surname: surname} #public method (note the =&gt; instead of -&gt;) walk: =&gt; msg += "#{@dogName} is walking\n" sayHello(@dogName, _private[@dogName].surname) #private method sayHello = (dog, surname) -&gt; msg += "Hi! says #{dog}. Call me #{surname}\n" window.ralph = new Dog("Ralph", "Ralphie") window.peter = new Dog("Peter", "Pitty") window.rex = new Dog("Rex", "Roxie") rex.walk() peter.walk() ralph.walk() alert msg </code></pre> <p>but you have to keep in mind that : _private grows with each new instance (it is a shared array) and your key (in this case @dogName) has to be unique accross instances.</p> <p><a href="http://coffeescript.org/#try%3amsg%20=%20%22Result-----------%5Cn%22%0A%0Aclass%20Dog%0A%20%20%20%20%0A%20%20#public%20variable%0A%20%20dogName%3a%20null%0A%20%20%20%20%0A%20%20#private%20variables%0A%20%20_private%20=%20%5B%5D%0A%20%0A%20%20constructor%3a%20%28@dogName,%20surname%29%20-%3E%0A%20%20%20%20_private%5B@dogName%5D%20=%20%7Bsurname%3a%20surname%7D%0A%0A%20%20#public%20method%20%28note%20the%20=%3E%20instead%20of%20-%3E%29%0A%20%20walk%3a%20=%3E%0A%20%20%20%20msg%20+=%20%22#%7B@dogName%7D%20is%20walking%5Cn%22%0A%20%20%20%20sayHello%28@dogName,%20_private%5B@dogName%5D.surname%29%0A%0A%20%20#private%20method%0A%20%20sayHello%20=%20%28dog,%20surname%29%20-%3E%0A%20%20%20%20msg%20+=%20%22Hi!%20says%20#%7Bdog%7D.%20Call%20me%20#%7Bsurname%7D%5Cn%22%0A%0Awindow.ralph%20=%20new%20Dog%28%22Ralph%22,%20%22ralphie%22%29%0Awindow.peter%20=%20new%20Dog%28%22Peter%22,%20%22pitty%22%29%0Awindow.rex%20=%20new%20Dog%28%22Rex%22,%20%22Roxie%22%29%0Arex.walk%28%29%0Apeter.walk%28%29%0Aralph.walk%28%29%0A%0Aalert%20msg%0A%0A" rel="nofollow">Try it</a></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.
    1. VO
      singulars
      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