Click to See Complete Forum and Search --> : Emulating private members when using prototype


mariano_donati
07-25-2008, 07:43 AM
Hi everyone. I need to make a code run faster and easier to read. The first thing I put my hand on this was to change all pseudoclasses defined within the classes's constructor to the form of classname.prototype = { object_definition };. Doing it in this way, I found my self in a trouble: I need to declare private methods. I've found a way for doing it:

classname.prototype = {
func1: function(){
function localfunc1(){ //func definition}
//rest of func1
},
func2: function(){
//I need to call localfunc1 from here too
}
}

With this structure I can define a localfunc1 function wich is not accessible from the outside, but it is accessible only from func1 and I also need to call it from func2. Is there a way of doing it?. Cheers.

Declan1991
07-25-2008, 08:38 AM
No, but depending on what you actually want to do, there are different ways to get around it. JavaScript doesn't really have "private members" at all, but by defining them is a closure (func1), you can pretend they exist. Have you actual code that you have a problem with, because there is probably a way to restructure it to avoid the problem.

Kostas Zotos
07-25-2008, 09:16 AM
Hi,

Maybe not exactly what you want..
May try something like this? (pass an index of target function, and just switch inside to the appropriate sub function):

function func1 (Index, Arg){

Functions=[localfunc1, localfunc2] // Set an array of your functions

if (!Arg) Arg=""

function localfunc1(Arg) {
return "Inner Function1\n"+Arg
}

function localfunc2() {
return "Inner Function2"
}

var Result=Functions[Index](Arg)
if (!Result ) Result=""

return Result

}



function func2 (){
alert ( func1 (0,"hello") ) // Call the first function and passing an argument
alert ( func1 (1) ) // Call the second function (with array index 1)
}


func2()

I did't examine it too much, just I thought the above idea..

PS: You may create also associative arrays, so that "call" the inner functions with their names, example:

Functions=[]

Functions['localfunc1']=localfunc1
Functions['localfunc2']=localfunc2

And call them like: alert ( func1 ('localfunc1', "hello") )

Cheers!

Kostas

mariano_donati
07-25-2008, 09:51 AM
To Declan1991: thanks for your good attitude at helping me out on this, but I have around 20-25 .js files that need to be restructured. I thought that may be would be a very straight-forward method to do it.

To Kostas Zotos: I really appreciate what you've written. I'll give it a chance. At the first moment I might say that I'll need a lot of time to restructure the code following the idea you mention above, for the same reason I said before: I have 20-25 .js files.

I think I'm gonna charge it a little bit more.
Thank you both. Regards.

Kostas Zotos
07-25-2008, 10:16 AM
Don't mention it!

Yes is a lot of work..

Maybe exists a method that we can't think at the present time..

It's a very good option if exist a such functionality..

Bye!

Kostas

rnd me
07-25-2008, 06:01 PM
it seems the solution posted complicates things quite a bit, and i see no advantage to it.

you could do it quite simply by wrapping an extra function around the whole thing, and you wouldn't have to heavily modify your code.
functions are closeable scope containers, take advantage.



function addClassProto(){

function secret(){ alert("private method here!"); }

classname.prototype = {
func1: function(){
function localfunc1(){ //func definition}
//rest of func1
},
func2: function(){
secret()
}
}

}//end addClassProto
addClassProto()

there is no way to make func1 hidden, you add it to the prototype. but, the prototype members can magically see the functions inside the addClassProto function (like secret), even though secret() will not be visible from the global scope. you could even use an anonymous function if you don't want addClassProto sticking around...

Kostas Zotos
08-16-2008, 01:34 PM
Hi,

Just a current similar topic reminds me this post..
If still search for a workaround the next maybe of some concern:

You can call functions defined inside other functions like this:
<script language="javascript">

// A function with 2 inner functions defined inside
function func1 (){

localfunc1= function(Arg) {
alert( "localfunc1: "+Arg )
}

localfunc2 = function() { // Note the way the function defined
alert( "localfunc2:" )
localfunc1( this )
}

}


// A second function from which call the inner functions in "func1"
function func2 (){
localfunc1( new Date() ) // Call the first function and passing an argument
localfunc2() // Call the second function
}


func1 () // IMPORTANT: NEED to call the " func1" function firstly, so the inner functions initialized

func2()

</script>

Bye!

Kostas