Created
December 28, 2011 11:57
-
-
Save PetrKaleta/1527710 to your computer and use it in GitHub Desktop.
Simple OOP pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log = (o) -> console.log o | |
# -------------- CLASS DEFINITIONS ------------- | |
# Private methods are starting with underscore, but this is just for better readability | |
class SimpleOOPPattern | |
@classProperty: 'le class property' | |
@classMethod: -> | |
'le class method' | |
publicProperty: 'le public property' | |
constructor: -> | |
log '### Simple inner calls ###' | |
# testing properties | |
log _privateProperty # => le private property | |
log @publicProperty # => le public property | |
log @constructor.classProperty # => le class property | |
# testing methods | |
log _privateMethod() # => le private method | |
log @publicMethod() # => le public method | |
log @constructor.classMethod() # => le class method | |
publicMethod: -> | |
'le public method' | |
# PRIVATE | |
_privateProperty = 'le private property' | |
_privateMethod = -> | |
'le private method' | |
# Inheritance, private members must be inaccessible | |
class ExtendedOOPPattern extends SimpleOOPPattern | |
constructor: -> | |
log '### Extended inner calls ###' | |
# testing properties | |
try _privateProperty catch e then log e.message # => Can't find variable: _privateProperty | |
log @publicProperty # => le public property | |
log @constructor.classProperty # => le class property | |
# testing methods | |
try _privateMethod() catch e then log e.message # => Can't find variable: _privateMethod | |
log @publicMethod() # => le public method | |
log @constructor.classMethod() # => le class method | |
# When calling original constructor, it should know all private members | |
class AnotherExtendedOOPPattern extends SimpleOOPPattern | |
constructor: -> | |
log '### Another extended inner calls from original constructor ###' | |
super # => works as expected | |
# -------------- CREATE INSTANCES -------------- | |
simple = new SimpleOOPPattern(); | |
extended = new ExtendedOOPPattern(); | |
new AnotherExtendedOOPPattern(); | |
# ------------ TESTING OUTER CALLS ------------- | |
log '### Simple outer calls ###' | |
# testing properties | |
log simple._privateProperty # => undefined | |
log simple.publicProperty # => le public property | |
log SimpleOOPPattern.classProperty # => le class property | |
# testing methods | |
try simple._privateMethod() catch e then log e.message # => 'undefined' is not a function (evaluating 'o._privateMethod()') | |
log simple.publicMethod() # => le public method | |
log SimpleOOPPattern.classMethod() # => le class method | |
log '### Extended outer calls ###' | |
# testing properties | |
log extended._privateProperty # => undefined | |
log extended.publicProperty # => le public property | |
log ExtendedOOPPattern.classProperty # => le class property | |
# testing methods | |
try extended._privateMethod() catch e then log e.message # => 'undefined' is not a function (evaluating 'o._privateMethod()') | |
log extended.publicMethod() # => le public method | |
log ExtendedOOPPattern.classMethod() # => le class method |
Very clear overview. I will bookmark this as a reference to OOP naming conventions in CoffeeScript.
Awesome, exactly what I needed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice summary.