inner classes proble

J

josh

Hi, I've the following problem:

If I have, for example,

BB =
{
CC =
{
makeDecision : function()
{
return "ko";
},

b : isOk ? "ok" : makeDecision()
}
}

now if I done


BB.CC.b where isOK == false;

than I have na error that makeDecision() is not defined

now is not possible to assign a variable value calling a function?

how can I make that?

Thanks
 
T

Tom de Neef

josh said:
If I have, for example,

BB =
{
CC =
{
makeDecision : function()
{
return "ko";
},

b : isOk ? "ok" : makeDecision()
}
}

now if I done


BB.CC.b where isOK == false;

than I have na error that makeDecision() is not defined

I am learning myself, so bear with me.
This has to do with the scope chain. The scope chain is altered when you
enter/leave a function. But you do not enter a function. You have an object
BB with property CC which is an object with two properties: makeDecision and
b. But 'entering' an object does not change the scope chain. BB.CC.b =
makeDicision() will be looking for makeDecision in the global execution
context. But it is not a property of the global variable and thus not
defined.
A solution would be to call BB.CC.makeDecision()

Tom
 
J

Joost Diepenmaat

josh said:
Hi, I've the following problem:

If I have, for example,

BB =
{
CC =
{
makeDecision : function()
{
return "ko";
},

b : isOk ? "ok" : makeDecision()
}
}

now if I done


BB.CC.b where isOK == false;

than I have na error that makeDecision() is not defined

Yes, because there isn't one. You just made BB.CC.makeDecision. Assuming
you meant BB = { C: { ... } } instead of BB = { C = { ... } }
now is not possible to assign a variable value calling a function?

Sure it is, but you cannot refer to a property of an object literal
within the object literal.

IOW: you can't do:

var o = {
f: 2;
g: o.f;
}

or anything similar.

In your case, assuming you actually want makeDecision to be a property
of BB.CC, you can do:


var BB = { CC : { makeDecision: function() { return "ko" } } };

BB.CC.b = isOk ? "ok" : makeDecision();
 
T

Thomas 'PointedEars' Lahn

Joost said:
In your case, assuming you actually want makeDecision to be a property
of BB.CC, you can do:


var BB = { CC : { makeDecision: function() { return "ko" } } };

BB.CC.b = isOk ? "ok" : makeDecision();

Probably you meant something along

with (BB) CC.b = isOk ? "ok" : CC.makeDecision();

because there would not be such a method in the scope chain.


PointedEars
 
J

Joost Diepenmaat

Thomas 'PointedEars' Lahn said:
Probably you meant something along

with (BB) CC.b = isOk ? "ok" : CC.makeDecision();

because there would not be such a method in the scope chain.

Erm, yeah that's what I meant. Although I wouldn't use with() in this
case.
 
T

Thomas 'PointedEars' Lahn

Joost said:
Erm, yeah that's what I meant. Although I wouldn't use with() in this
case.

What better case is there? You know which properties the object has (so
`isOk' could not be looked up there), and you use the reference twice in a
statement.


PointedEars
 
J

Joost Diepenmaat

Thomas 'PointedEars' Lahn said:
What better case is there? You know which properties the object has (so
`isOk' could not be looked up there), and you use the reference twice in a
statement.

It's mostly a matter of taste, but I'd probably use it only if I was
dealing with a few (or a lot) more properties. The only place I've used
with() at all recently was with some code that had generator functions
for each HTML element (and I didn't want to make all of them global).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top