[Javascript] accessing nested functions

Pedro Mpa mail.pmpa at sapo.pt
Sat Jul 7 10:42:10 CDT 2007


Thanks for replying, please apologise my bad English.

If I add "new" to function declaration, when I call "test = new a();" the
code inside function b is executed too, and that’s not what I intended. I
was looking for the PHP class->function type of behaviour but with function
nesting which isn't allowed in PHP.

function a(){
    alert('a');

    this.b = new function(){
        alert('b');

        this.c = function(){
            alert('c');
        }
    }
}

foo = new a();
> alert a
> alert b


Sorry for top posting :)
Pedro.

-----Original Message-----
From: javascript-bounces at LaTech.edu [mailto:javascript-bounces at LaTech.edu]
On Behalf Of Edwin Martin
Sent: sábado, 7 de Julho de 2007 8:35
To: [JavaScript List]
Subject: Re: [Javascript] accessing nested functions

Pedro Mpa wrote:
> I'm having some trouble accessing nested functions, like in the following
> example, how can I access function c ?
>
> function a(){
> 	this.vara = "";
>
> 	this.b = function(){
> 		this.varb = "";
>
> 		this.c = function(){
> 			this.varc = "";
> 			alert('something');
> 		}
> 	}
> }
>
> Tried using:
>
> test = new a();
> test.b.c();
>   
c is only assigned to b if you execute b. So thiw works:

function a(){
    this.vara = "";

    this.b = new function(){
        this.varb = "";

        this.c = function(){
            this.varc = "";
            alert('something');
        }
    }
}


test = new a();
test.b.c();

The only change is in the line this.b = new function(){
>   
>> test.b.c() Is not a function.
>>     
>
> Is there a limit for nesting functions in JS ?
>   
I don't believe there is.

Edwin Martin

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript






More information about the Javascript mailing list