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 dummy.py and you do something like this:

>>> from dummy import *
>>> 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:

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%!

Anonymous - 30 April 2006 [«« Reply to this]
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.
Simon - 30 April 2006 [«« Reply to this]
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: http://www.crockford.com/javascript/private.html).

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 (http://www.google.com/search?q=javascript%20obfuscator).
Peter Bengtsson - 01 May 2006 [«« Reply to this]
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