Acceptable or not, it won't work.
private
will be undefined
, and private function foo(){}
will throw a SyntaxError: Unexpected token function
.
Don't try to mold JavaScript into the conventions of a language it's not.
Domanda
While programming a server application for multiplayer games, I got an idea for cleaning up my code a bit.
I'm heavily using the Module Pattern
for my application and use a convention for calling exposed functions public[Function name]
and privates private[Function name]
. The public
prefix helps in the return assignment:
return {
get : publicGet
}
But the private identifier names are just for show and make calling them a bit cumbersome. However, completely removing the private
prefix makes the code a bit less readable. So I thought of using the reserved private
keyword as you would in, for example, Java.
For doing this I thought I could use eval
like this:
var private = eval('') // Just an empty assignment so it does nothing.
So I can write my private function like this:
private function initiateGameMatchmaker(game) {
// Code...
}
Instead of:
function privateInitiateGameMatchmaker(game) {
// Code...
}
Would this be acceptable or do you regard this as a filthy hack and thus as a definite no-no?
Soluzione
Acceptable or not, it won't work.
private
will be undefined
, and private function foo(){}
will throw a SyntaxError: Unexpected token function
.
Don't try to mold JavaScript into the conventions of a language it's not.