Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript Class Members

Tags:

coffeescript

I am new to CoffeeScript (and rather inexperienced with JS too; so sorry if this is naive) and I was trying to create a class as below:

class Test
   a: []

   make: ->
       @a.push ['A', 'B', 'C']

   getdata: ->
       output = ""
       for i in @a
          output += i
       output

b = new Test
b.make()

alert(b.getdata())


c = new Test
c.make()

alert(c.getdata())

The output I get is: "A, B, C" "A, B, C, A, B, C"

Despite create a new instance of 'Test'; the array gets appended over and is not cleared. What am I doing wrong here? Am I initializing the member variable wrong?

like image 632
Anoop Avatar asked Dec 02 '11 11:12

Anoop


People also ask

What is class member and instance member?

A member is either a property or a method. An instance member is a member of an instance of a class or a struct; sometimes folks say member when you they mean instance member, but it's best to only do that when the context is clear. A class member is a member of a class rather than an instance.

What is CoffeeScript used for?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

What is class member variable in Java?

In class-based programming languages, these are distinguished into two types: class variables (also called static member variables), where only one copy of the variable is shared with all instances of the class; and instance variables, where each instance of the class has its own independent copy of the variable.

What is a JavaScript instance variable?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.


1 Answers

When you define a: [], you're creating a single array object on the class prototype. Each instance of the class that you create will have this same array object. Whenever one instance modifies the value, the change is visible to all the others.

Note that this is only the case if you modify a value, such as by adding items to an array. If you replace the value, for example by assigning a new array, this will only affect the current instance.

When you want a property that's initialized on a per-instance basis you should to define it in the constructor, when the instance is actually created:

class Test
   constructor: ->
       @a = []
       @a.push ['A', 'B', 'C']

   getdata: ->
       output = ""
       for i in @a
          output += i
       output

b = new Test

alert(b.getdata())


c = new Test

alert(c.getdata())

Try this out and you'll find that it works like you want.

like image 130
Jeremy Avatar answered Oct 08 '22 01:10

Jeremy