CoffeeScript Encapsulation and Variable Access -


trying understand how coffeescript instance , class variable works came code (and results in comments).

class   x: 1   @y: 2    constructor: (@z) ->      #console.log "const x", x #referenceerror: x not defined     console.log "constructor y", @y #undefined     console.log "constructor z", @z # = 3 , 6 b    get: () ->      #console.log "get x", x #referenceerror: x not defined     console.log "get y", @y #undefined     console.log "get z", @z # = 3 , 6 b    get2: () =>      #console.log "get2 x", x #referenceerror: x not defined     console.log "get2 y", @y #undefined     console.log "get2 z", @z # = 3 , 6 b    @get3: () ->      #console.log "get3 x", x #referenceerror: x not defined     console.log "get3 y", @y # = 2     console.log "get3 z", @z #undefined    @get4: () =>      #console.log "get4 x", x #referenceerror: x not defined     console.log "get4 y", @y # = 2     console.log "get4 z", @z #undefined  class b extends   constructor: (@w) ->     super(@w)  console.log '------a------' = new 3 console.log "i.x", i.x # = 1 console.log "i.y", i.y #undefined console.log "i.z", i.z # = 6 i.get() i.get2() a.get3() a.get4() console.log '------b------' = new b 6 console.log "i.x", i.x # = 1 console.log "i.y", i.y #undefined console.log "i.z", i.z # = 6 console.log "i.w", i.w # = 6 i.get() i.get2() b.get3() b.get4() console.log '------------' 

there strange things happening here:

  1. x var expecting access method x var can't accessed method or constructor (referenceerror). i'm able access instance of or b (i.x). why that?

  2. @y var expecting @y var value method has no value in of places (undefined value, not referenceerror exception). @y has value on @get3 , @get4 (instance methods?). if defined, why can't value?

  3. @y , @z var both @y , @z instance variables, because @z initialized in constructor, has differentiated behavior. @y valid on @get3 , @get4 , @z valid on , get2. again, happening here?

the thing i'm confused these behaviors. code correct? so, should learn more js generated cs?

tks

in function bodies, @ refers this , in class definitions, @ refers class rather prototype.

so in example above, definition of @y refers a.y, , not a.prototype.y. it's tricky refer because of way this bound in various ways of defining methods. can access using @y methods named @get because in case this refers a.

the definition of x refers a.prototype.x , get methods should access via @x in get1 , get2.

as basic guide, try not use @ outside of function bodies , make lot more sense:

class   constructor: (@a) ->   b: 2   trystuff: =>     console.log(@a) #will log whatever initialized in constructor     console.log(@b) #will log 2 

edit: consider methods defined @something static methods of class, can call them classname.something() static methods, can't access instance variables.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -