variable-scope

Javascript object property inaccessible from another property (function)

Hi, Quick question, why does my reference to weekdays inside change_date() give weekdays is undefined error in Firebug? I also tried this.weekdays, same. How do I correct this? var timesheet_common = { weekdays : ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"], change_date: function() { $('#text_input').val( weekdays[(n...

Is it wrong to use braces for variable scope purposes?

Hi, I sometimes use braces to isolate a block of code to avoid using by mistake a variable later. For example, when I put several SqlCommands in the same method, I frequently copy-paste blocks of code, ending by mixing the names and executing twice some commands. Adding braces helps to avoid this situation, because using a wrong SqlComm...

PHP global variable is undefined inside a function even if global keyword is used

Yes I know global variables is a bad practice, but ease up on that rule for this one :P My code: include('something.php'); //where $from is declared function myfunc() { global $from; echo "from(myfunc)=$from<br />"; ... } echo "from=$from<br />"; myfunc(); The result is: from=2010-05-01 from(myfunc)= What's going on?...

Perl, using variable from within While loop outside of the loop?

This seems really simple but it is giving me a hard time figuring it out as I'm new to perl.. I've been looking through a lot of documentation now about loops and I am still stumped by this... I have a sub that contains a where loop and I want to use a variable value from within the loop outside of the loop (after the loop has run), howe...

defining variable from string

I'm trying to define variable inside function. vars() shows variable is created, but gives me NameError: exception. What am I doing wrong? def a(str1): vars() [str1] = 1 print vars() print b a('b') output: {'str1': 'b', 'b': 1} exception: NameError: global name 'b' is not defined ...

scope of variables when using for-loop in java - eclipse / compiler error?

I've written the following code: for(int layer = 0; layer <countLayers; layer++); { List<Sprite> spritesInLayer = sceneGraph.getLayer(layer); } when i compile this snippet, I get an error, that in the line within the for-Loop, eclipse complains that 'layer' is an unknown symbol [... = sceneGraph.getLayer(layer);] and wants me to i...

Limiting variable scope

I'm trying to write a function, which limits the scope of R variables. For example, source("LimitScope.R") y = 0 f = function(){ #Raises an error as y is a global variable x = y } I thought of testing the variable environment, but wasn't really sure of how to do this. The why I teach R to undergrads. In their first couple of ...

Is it proper to use the term "global" in a relative sense?

Please assume the following contrived JavaScript: function do_something() { var x = 5; function alert_x() { alert(x); } alert_x(); } do_something(); The variable x is local to the function do_something. It isn't a global variable because it's not available in every scope (i.e., outside of either of the functions, such...

Javascript scope question - chrome extension

for(var p = 0; p < xmls.length; p++) { var feed = new google.feeds.Feed(xmls[p]); feed.load(function(result) { //code In the above code, if I output p outside of the feed.load function, I correctly see that p iterates. However, if I output p inside that function (where I need to access it), It remains at 2 (which happens to be...

C# scope question

Consider the following code sample: // line # { // 1 // 2 { // 3 double test = 0; // 4 } // 5 // 6 double test = 0; // 7 } // 8 This gives the error ...

What is the search order for free variables in Python?

Specifically, how are free variables bound at definition for methods of a class? It is probably something like this: enclosing function (temporary) scope => generate closure global (permanent) scope => generate no closure (just look it up when the method body executes) raise UnboundLocalError() Here are two examples: globalname = 0...

Scope and Asynchronous JavaScript

I recently ran into an issue at work in which, at least according to my knowledge of JavaScript, I got back an impossible result. I'm hoping someone can explain whats going on here and why the actual results differ from my expected results. Expected results in console id: a , x: 1 id: b , x: 1 id: c , x: 1 Actual results in console ...

JavaScript OOPS Question

Hey guys, A JavaScript newbie here. I have this following code: function testObject(elem) { this.test = "hi"; this.val = elem; console.log(this.test+this.val); echo(); function echo () { console.log(this.test+this.val); } } var obj = new testObject("hello"); When it is run, I expect "hihello" to be ...

Way in Python to make vars visible in calling method scope?

I find myself doing something like this constantly to pull GET args into vars: some_var = self.request.get('some_var', None) other_var = self.request.get('other_var', None) if None in [some_var, other_var]: logging.error("some arg was missing in " + self.request.path) exit() What I would really want to do is: pull_args('some_...

PHP - How to be able to use variables inside __autoload() function

The autoload function I am using is as follows:- function __autoload($moduleName) { //Logic to check file existence- include if exists else redirect to fallback page } Does it not take any other arguments? I want to perform some logic based on some variables inside the autoload function. How do I do it without the use of global ...

PHP subclass can't access public variable set by parent

Hello, I'm very new to PHP and OOP in general. I'm using codeigniter for a framework, and am currently attempting to build a class 'BuildLinks' that will redirect the user to the correct link based on what URL they landed on. The controller passes the right variables to the class, while the function build_afflink() selects what class ...

What is the scope of the counter variable in a for loop?

I get the following error in Visual Studio 2008: Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else This is my code: for (int i = 0; i < 3; i++) { string str = ""; } int i = 0; // scope error strin...

PHP function call not working - presumed casting problem?

Why does THIS not work when called via e.g. wd(1) (it always returns '-2')? $zoom=$user['zoom']; function wd($hrs){ return $hrs*$zoom-2; } But THIS works fine: function wd($hrs){ return $hrs*30-2; } Assuming this was a casting problem, I tried all sorts of variations like (int)$hrs * ((int)$zoom) or (int)$hrs * (float)$zoom...

JavaScript variable scopes

I found a bunch of other questions about this topic, but for some reason they did not solve my "problem". I have this little script I made of pure interest - and it works just fine, but something is bothering me. The script clears a text field onFocus and types "Write here" if nothing is entered onBlur. The script is as follows: <html>...

Can base classes see the protected fields of derived classes?

I don't know if this has to do with how FindControl works or how scope works. But my base class is having a hard time seeing the fields of child classes. Currently I'm planning have the derived class set a property in the base class, but there are a lot of derived classes, so that isn't a very attractive solution. public class BaseP...