Private functions in Javascript?

29 April 2006   3 comments   Python, Web development

Powered by Fusion×

In Python you indicate whether a function/method is private by naming it so that it starts with an _ underscore like this:

def _thisIsPrivate():
    return 'a'

def thisIsPublic():
    return 'b'

It means that if you have these two functions in a file called and you do something like this:

>>> from dummy import *
>>> thisIsPublic()
>>> _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:

function addParameters(param1, param2) {
   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 addParameters(_0,_1){return _a(_0)+_a(_1);}
function subtractParameters(_0,_1){return _a(_0)-_a(_1);}
function _a(_0){return _0.toUpperCase();}

That's a bandwidth save of 123 bytes 45%!


maybe have different 'hardcore' modes? --hardcore=1 would change local names and argument names, but --hardcore=2 would slim _ named functions. Or you could allow custom regexes for the functions to allow slimming.
Since JavaScript has no notion of a module, there is can be no "private" functions as used in your Python example (which aren't that private either: "from dummy import _thisIsPrivate; _thisIsPrivate()"). And I've never really seen functions beginning with underscores outside of OO-like programming (where technically private functions are possible:

As for your slimmer: what about simply testing for the existence of a function of the slimmed name first? And for inspiration, you might want to have a look at other products with the same functionality (
Peter Bengtsson
That's a very interesting read. I figured what one can do is to relabel all functions that are repeated by generating new code in the slimmer. So if you start with this code::

function parseSomething(z) {
function foo(x, y) {
return parseSomething(x) + parseSomething(y);

Then this can be revamped to this::

function A(z) {
function foo(x, y) {
return A(x) + A(y);
var parseSomething=A;

Not a big saving, but at least something.

Your email will never ever be published

Related posts

type - Writing shell scripts 28 April 2006
Next: 02 May 2006
Related by keywords:
Newfound love of @staticmethod in Python 02 July 2012
Smooth anchor scrolling Javascript 17 February 2006
Optimize with 15 February 2005
XHTML, HTML and CSS compressor 07 April 2004
parametrize_url() adding parameters to URLs 14 January 2005
Comparing YUI Compressor and slimmer 17 November 2009
slim, a new free web service for white space optimisation 25 July 2006
Slimmer with --hardcore 06 May 2006