JavaScript and Variable Scope Hacks

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

JavaScript and Variable Scope Hacks

Post 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 :)
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: JavaScript and Variable Scope Hacks

Post 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 :D , 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();
    
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: JavaScript and Variable Scope Hacks

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: JavaScript and Variable Scope Hacks

Post 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(); 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: JavaScript and Variable Scope Hacks

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: JavaScript and Variable Scope Hacks

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: JavaScript and Variable Scope Hacks

Post 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? :)
Post Reply