jashkenas/coffee-script — Issue #1854: Class properties are not deep cloned on extension
Author: devongovett
Created: 2011-11-11 03:25:11+00:00
State: open

It is sometimes annoying that array and object class properties are not deep cloned upon extending the class. This leads to issues where modifying that property on the subclass also modifies it on the super class. For example:

```coffeescript
class Test
@test = []

class Foo extends Test

Foo.test is Test.test #=> true
```

I think that either CoffeeScript should deep clone those objects (probably not), or that this is a case for the [extended hook](https://github.com/jashkenas/coffee-script/issues/841) to return. Here is what I do currently: since I have a class method that uses my class level property, I do something like this:

```coffeescript
class Test
@test = []

@doSomething: ->
if @test is @__super__.constructor.test
@test = @test.slice() # clone it

# do something...
```

A dirty hack indeed, and I have to do that on each method that uses `@test`. The extended hook would allow me to do that cloning process when the class is extended and avoid this. For example:

```coffeescript
class Test
@test = []

@extended: ->
@test = @test.slice() # clone

@doSomething: ->
# do something...
```

Much cleaner. Thoughts?