How to make method "btnClick" private?
class FirstClass
constructor: ->
$('.btn').click @btnClick
btnClick: =>
alert('Hi from the first class!')
class SecondClass extends FirstClass
btnClick: =>
super()
alert('Hi from the second class!')
@obj = new SecondClass
There's no private in JavaScript so there's no private in CoffeeScript, sort of. You can make things private at the class level like this:
That
private_function
will only be visible withinC
. The problem is thatprivate_function
is just a function, it isn't a method on instances ofC
. You can work around that by usingFunction.apply
orFunction.call
:So in your case, you could do something like this:
Demo: http://jsfiddle.net/ambiguous/5v3sH/
Or, if you don't need
@
inbtnClick
to be anything in particular, you can just use the function as-is:Demo: http://jsfiddle.net/ambiguous/zGU7H/