I am trying to understand how private methods are created using coffeescript. Following is an example code
class builders
constructor: ->
// private method
call = =>
@priv2Method()
// privileged method
privLedgeMethod: =>
call()
// privileged method
priv2Method: =>
console.log("got it")
Following is the generated JS code.
(function() { var builders, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; builders = (function() { var call, _this = this; function builders() { this.priv2Method = __bind(this.priv2Method, this); this.privLedgeMethod = __bind(this.privLedgeMethod, this); } call = function() { return builders.priv2Method(); }; builders.prototype.privLedgeMethod = function() { return call(); }; builders.prototype.priv2Method = function() { return console.log("got it"); }; return builders; }).call(this); }).call(this);
Note that I have used "fat arrow" for the function definition. There are couple of things that I didn't get from the code.
- what's the use of _this variable
- if you run this code as : (new builders()).privLedgeMethod() than inside the call method it doesn't find priv2Method method. Even though the builders object does show priv2Method as a property of it's prototype.
Hopefully someone could shed some light here.