Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance properties #82

Open
DawidMyslak opened this issue Jun 5, 2020 · 1 comment
Open

Instance properties #82

DawidMyslak opened this issue Jun 5, 2020 · 1 comment

Comments

@DawidMyslak
Copy link

According to the book (https://arcturo.github.io/library/coffeescript/03_classes.html) instance properties can be created in the following way:

class Animal
  price: 5 # instance property according to the book
  sell: (customer) ->

animal = new Animal
animal.sell(new Customer)

But when I checked generated JavaScript code it looks like price property was added to the prototype 😕...

Well, if you try to use it I guess in that case it will magically work because the number type in JavaScript is immutable, but let me just show you an example with mutable field:

class Store
 products: [] # instance property?

 addProduct: (name) ->
  @products.push(name)

foodStore = new Store
foodStore.addProduct 'milk'

motoStore = new Store
motoStore.addProduct 'tyre'

console.log motoStore.products # ['milk', 'tyre'], boom

So obviously products property was added to the prototype and it's shared by both instances.

@DawidMyslak
Copy link
Author

Oh, I've just noticed the same issue was already raised here: #80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant