What on earth is going on here?
How can the Daughter-class contain data that’s been set in Brother, and how should I make sure that data set in sibling classes doesn’t interfere with eachother?
class Parent
data: {}
class Child extends Parent
age: 10
Son = new Child
Son.data.name = "John Doe"
Daughter = new Child
console.log Daughter.data # => { name: 'John Doe' }
If you take your CS code and put it in the side-by-side editor on CoffeeScript’s site (http://coffeescript.org/), you’ll see that
datais on the prototype of theParent“class”. Think of that prototype being the template for new functions (classes) that you create. That prototype either contains other functions or it contains variables available to all instances (like an OO static variable).You’re adding
nameto a “static” variabledata. It will then be available to all instances that “derive” fromParent.I’m not sure of the inner workings that go on there, but coming from an OO world, that’s how I interpret it. I hope this helps.
GENERATED CODE VIA COFFEESCRIPT’S SITE
UPDATE
Just noticed that the CS side-by-side window has a link feature. Here’s a link to the side-by-side code.
UPDATE #2
In response to your question in the comments, I’m not sure exactly what you want to do, but you could do something like this:
Perhaps keep the
namevariable (or the entiredatavariable) at the parent level and set it via a constructor and access it via a parent function.