
Private functions in Javascript?
29th of April 2006
In Python you indicate whether a function/method is private by naming it so that it starts with an _ underscore like this:
return 'a'
def thisIsPublic():
return 'b'
It means that if you have these two functions in a file called dummy.py and you do something like this:
>>> thisIsPublic()
'a'
>>> _thisIsPrivate()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name '_thisIsPrivate' is not defined
Seems fair doesn't it. Now is there such similar naming convention and functionality in Javascript?
In many of my javascript files there will be functions with the same naming convention as Python so that I can remember which functions might be used from outside and which are only relevant inside the module's scope. But that's just me and I don't think it has an actual effect other than my personal naming convention taste.
The reason I'm asking is that I want to improve my slimmer with the new --hardcore option. Last month I added that if you switch on --hardcore it renames all parameters to functions from something like document_node to _1 and formname to _2. Now I want to do something similar to the functionnames too. So imagine going from:
return _fixParameter(param1) + _fixParameter(param2);
}
function subtractParameters(param1, param2) {
return _fixParameter(param1) - _fixParameter(param2);
}
function _fixParameter(param) {
return param.toUpperCase();
}
to this much more whitespace efficient result:
function subtractParameters(_0,_1){return _a(_0)-_a(_1);}
function _a(_0){return _0.toUpperCase();}
That's a bandwidth save of 123 bytes 45%!
Comment
Show all 3 commentsCommenting is currently disabled in Mobile version