M
My Pet Programmer
Steve Howell said:
The constructor I got was:
function World {
I sent a couple links over, did you get those? I haven't seen them post
to the list, but they give you a much better idea of what you're dealing
with here:
http://javascript.crockford.com/private.html
Is a really good one.
~A!
Steve Howell said:
As written, that code does not actually work.function World {
/* snip */
function wall_on_north(x, y)
{
if (y == 0 || y == this.num_streets) {
return true;
}
coords = x + "," + y;
return (north_walls[coords] == 1);
}
}
The method wall_on_north really does work, but I snipped out too much
context.
Giving some more context, but still striving for some brevity here:
function World() {
/* snip */
var north_walls = [];
this.num_streets = 10;
function wall_on_north(x, y)
{
if (y == 0 || y == this.num_streets) {
return true;
}
coords = x + "," + y;
return (north_walls[coords] == 1);
}
this.wall_on_north = wall_on_north;
}
the_world = new World();
First of all, forget about how wall_on_north is called from outside
World. It's created a bunch of red herrings that have nothing to do
with my question.
Next, look at the use of num_streets and north_walls above. With both
variables I have the inner function refer to variables that are to
meant to be within the scope of World(). In one case, num_streets, I
use "this" to make the scope explicit. In the other case,
"north_walls," I let JS figure out the scope.
(As for x and y, they're passed in parameters, and as for coords, it's
just a local variable.)
My question is fairly simple--what is the best practice for referring
to variables so that it's clear they're in the scope of World, i.e.
their scope is more than local to the inner function? My original
code had "this," but then David seemed to be suggesting that was
somehow wrong.
function World() { is a
better first line, but accepting that....
You say "function World() {" is a better first line...a better first
line than what? Isn't that what I showed?
The constructor I got was:
function World {
I sent a couple links over, did you get those? I haven't seen them post
to the list, but they give you a much better idea of what you're dealing
with here:
http://javascript.crockford.com/private.html
Is a really good one.
~A!