Page 1 of 1
JavaScript and Variable Scope Hacks
Posted: Wed Jan 14, 2009 10:36 pm
by Chris Corbyn
Anyone know if there's a hack to make something like this work?
[js]var callback = function callback() { alert(x);}; function foo() { var x = 7; callback(); //Wanting callback to have access to x}; foo();[/js]
It's for a parser where variables will be available to a function depending upon the state of the parser. I sort of expected the above to work, but I guess the function remains in the scope in which it was defined. Knowing how funk JS is, does anyone know if there's some sort of hack to get the above to work? I thought about something with "new Function(stringVersionOfOtherFunction)" but couldn't get that to work.
EDIT | I do have a back-up plan of just passing in arguments, but I'm curious more than anything

Re: JavaScript and Variable Scope Hacks
Posted: Wed Jan 14, 2009 10:57 pm
by novice4eva
lose the var
Code: Select all
function foo() {
x = 7;
callback(); //Wanting callback to have access to x
};
EDIT: Actually i have been reading on OOP approaches for java script for past 2 days now, since i came to know of cappucino(sorry if i miss-spelled it) from you and my knowledge of java script being far more less that yours or any other mods here

, please let me know if i am being officious but this was something new to me(not necessarily to others) so here is what i did
Code: Select all
function callback() {
alert(this.x);
};
function foo() {
this.x = 7;
this.callback = callback;
obj.callback();
}
obj = new foo();
Re: JavaScript and Variable Scope Hacks
Posted: Thu Jan 15, 2009 12:26 am
by Chris Corbyn
Yep, that's perfectly valid

JavaScript (ECMAScript actually) is quite different to other OO languages in the way it deals with inheritance. This particular tricks comes from the fact that "this" changes depending upon context.
Re: JavaScript and Variable Scope Hacks
Posted: Thu Jan 15, 2009 2:07 pm
by Weirdan
If calling function is a part of some object, it could opt to call the external function in the context of current object, kinda like mixins in ruby:
Code: Select all
var a = { a : function() { console.log(this.q); } }; var b = { b : function() { this.q = "property of b"; a.a.apply(this); } }; b.b();
Unfortunately both the Function constructor arguments and eval arguments are evaluated in the global scope, so I see no way to allow access for external function to local scope.
Instead of applying the function in the context of current object, you could apply in the context of an object specifically created to pass you local scope. This would imply enumerating your scope manually though, so it would be quite similar to merely passing required arguments:
Code: Select all
var a = { a : function() { console.log(this.q); } }; var b = { b : function() { var q = "property of b"; a.a.apply({ 'q' : q }); } }; b.b();
Re: JavaScript and Variable Scope Hacks
Posted: Fri Jan 16, 2009 10:43 pm
by Chris Corbyn
Thanks guys. I think I'm just gonna pass them as arguments. It's clean and it's easier to test. The function will always know how many arguments it will need.
Re: JavaScript and Variable Scope Hacks
Posted: Fri Jan 16, 2009 11:08 pm
by JAB Creations
Chris, while I'm not exactly sure what you guys are doing I'm simply setting an input's value to keep track of message ID's in my chat room. The row ID's are essentially 'row_' + the MySQL row ID. It's a bit cheap though it does work. What do you mean by the state of the parser?
Re: JavaScript and Variable Scope Hacks
Posted: Wed Jan 21, 2009 8:51 pm
by Chris Corbyn
~JAB Creations, I'm building a parser (and a FSM), I'm not using an existing one
I forgot about with() { ... }. While it doesn't allow me to change scope externally, it does allow me to bring commonly accessed members into the current scope.
[js]var foo = { prop: 57, func : function () { print("In func()"); }} with (foo) { print(prop); func();}[/js]
Code: Select all
java -jar js.jar -f test.js
57
In func()
PS: Did I mention I'm really enjoying Mozilla's Rhino project for a Pure ECMAScript interpreter?
