Skip to content

Instantly share code, notes, and snippets.

@nshtg
Last active August 29, 2015 14:02
Show Gist options
  • Save nshtg/cd00148ed8fd4ac52a7d to your computer and use it in GitHub Desktop.
Save nshtg/cd00148ed8fd4ac52a7d to your computer and use it in GitHub Desktop.
Public and private vars, class variables and functions...
class Foo
PublicProperty: "I am public"
_privateVariable = "I am private"
@classConst = "I'm from the class mister"
constructor: ->
@publicPropertyToBeSet = "lopl"
publicMethod: -> @PublicProperty
_privateMthod = -> _privateVariable
getCallPrivateMthod: -> _privateMthod()
getPrivateVar: -> _privateVariable
@classGetConst: -> @classConst
getClassConst: -> @constructor.classConst
getClassConstE: -> Foo.classConst = "Can you handle me"
modifyTheClass: -> @constructor::getClassConst = -> "You ain't getting anything from me!"
c = new Foo()
d = new Foo()
console.log c.PublicProperty # => "I am public"
console.log c.publicMethod() # => "I am public"
console.log c._privateVariable # => "undefined"
#console.log c._privateMthod() # ERROR
console.log c.getCallPrivateMthod() # => "I am private"
console.log c.getPrivateVar() # => "I am private"
console.log c.publicPropertyToBeSet # => "lopl"
console.log Foo.classConst # => "I'm from the class mister"
console.log Foo.classGetConst() # => "I'm from the class mister"
console.log c.getClassConst() # => "I'm from the class mister"
console.log c.getClassConstE() # => "Can you handle me"
console.log c.getClassConst() # => "Can you handle me"
c.modifyTheClass()
console.log c.getClassConst() # => "Can you handle me"
console.log d.getClassConst() # => "Can you handle me"
d.constructor::getClassConst = -> "You won't stop trying, do you?"
console.log c.getClassConst() # => "You won't stop trying, do you?"
console.log d.getClassConst() # => "You won't stop trying, do you?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment