variable-scope

Changing a Variable Out of Scope?

Is there any way to change a variable while out of scope? I know in general, you cannot, but I'm wondering if there are any tricks or overrides. For example, is there any way to make the following work: function blah(){ var a = 1 } a = 2; alert(blah()); EDIT (for clarification): The hypothetical scenario would be modifying a variabl...

When I overload the assignment operator for my simple class array, I get the wrong answer I expect

//output is "01234 00000" but the output should be or what I want it to be is // "01234 01234" because of the assignment overloaded operator #include <iostream> using namespace std; class IntArray { public: IntArray() : size(10), used(0) { a= new int[10]; } IntArray(int s) : size(s), used(0) { a= new int[s]; } int& operator[]( int...

FF extension: global variable that persists over multiple instances of the browser untill reset?

Can we use a Global variable that persists over multiple instances of the browser (FF)? I am building a ff extension which has to take the host& port name from the user once and then execute the menu options accordingly using that very host and port. This Host and port must remain same untill the user resets it (for which an option will ...

Can't add object to Array in jQuery's getJSON data function (scope issue)

I have a person object and wants to store it into a global ArrayCollection I have made. Works great in normal scope: var s = new ArrayCollection(); s.add(new person("Knud", "Mikkelsen", 35)); The problem is when I want to add people inside my jQuery function "mainFunction". I can't seem to get it right. I know it's something to do wi...

Mutable global variables don't get hide in python functions, right?

Please see the following code: def good(): foo[0] = 9 # why this foo isn't local variable who hides the global one def bad(): foo = [9, 2, 3] # foo is local, who hides the global one for func in [good, bad]: foo = [1,2,3] print('Before "{}": {}'.format(func.__name__, foo)) func() print('After "{}": {}'.format(...

JS: variable inheritance in anonymous functions - scope

hey guys, someone from doctype sent me here. long story short: var o="before"; x = function() //this needs to be an anonymous function { alert(o); //the variable "o" is from the parent scope }; o="after"; //this chages "o" in the anonymous function x(); //this results in in alert("after"); //which is not the way i want/need it in r...

What's the scope of a Python variable declared in an if statement?

I'm new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == '__main__': x = 1 print x In other languages I've worked in, this code would throw an exception, as the x variable is local to the if statement and should not exist outside of it....

how to access a firefox extension variable from the current document/window

my firefox extension has an object myExt . myExt = { request: function(){ //adds dynamic script element to the current webpage's head tag }, callback: function(json) { //do something with this } }; myExt.request adds a dynamically added script element to a server that returns json, i want the json to be sent to myExt.c...

Variable Scoping in a method and its persistence in C++

Consider the following public method that adds an integer variable to a vector of ints(private member) in a class in C++. KoolMethod() { int x; x = 10; KoolList.Add(x); } Vector<int>KoolList; But is this a valid addition to a vector ??? Upon calling the method, it creates a local variable. The scope of this local variable ends t...

unique random id

I am generating unique id for my small application but I am facing some variable scope problem. my code- function create_id() { global $myusername; $part1 = substr($myusername, 0, -4); $part2 = rand (99,99999); $part3 = date("s"); return $part1.$part2.$part3; } $id; $count=0; while($count == 1) { $id; $id=create_id...

Help needed with JavaScript variable scope / OOP and call back functions

I think this issue goes beyond typical variable scope and closure stuff, or maybe I'm an idiot. Here goes anyway... I'm creating a bunch of objects on the fly in a jQuery plugin. The object look something like this function WedgePath(canvas){ this.targetCanvas = canvas; this.label; this.logLabel = function(){ console.log(th...

What is the most idiomatic way to emulating Perl's Test::More::done_testing?

I have to build unit tests for in environment with a very old version of Test::More (perl5.8 with $Test::More::VERSION being '0.80') which predates the addition of done_testing(). Upgrading to newer Test::More is out of the question for practical reasons. And I am trying to avoid using no_tests - it's generally a bad idea not catching w...

JQuery will not set a higher scoped object in callback

Hello, I have a jquery callback function. In that function I want it to change the value of a varible that is in a higher scope, for somereason it is not doing that. Here is the code. Thanks function add() { var returnedData = { my_id: 0 }; $.post("add_event.php", { event : toSendText }, function(data) { re...

Idiomatic ruby for temporary variables within a method

Within a method, I am using i and j as temporary variables while calculating other variables. What is an idiomatic way of getting rid of i and j once they are no longer needed? Should I use blocks for this purpose? i = positions.first while nucleotide_at_position(i-1) == nucleotide_at_position(i) raise "Assumption violated" if i == 1 ...

Which of the following Java coding fragments is better?

This isn't meant to be subjective, I am looking for reasons based on resource utilisation, compiler performance, GC performance etc. rather than elegance. Oh, and the position of brackets doesn't count, so no stylistic comments please. Take the following loop; Integer total = new Integer(0); Integer i; for (String str : string_list) {...

Scope of variables inside anonymous functions in C#

I have a doubt in scope of varibles inside anonymous functions in C#. Consider the program below: delegate void OtherDel(int x); public static void Main() { OtherDel del2; { int y = 4; del2 = delegate { Console.WriteLine("{0}...

Variable scope difference between PHP and C: block scope is not exactly the same?

The following PHP code will output 3. function main() { if (1) { $i = 3; } echo $i; } main(); But the following C code will raise a compile error. void main() { if (1) { int i = 3; } printf("%d", i); } So variables in PHP are not strictly block-scoped? In PHP, variables defined in inner blo...

How to structure javascript callback so that function scope is maintained properly

I'm using XMLHttpRequest, and I want to access a local variable in the success callback function. Here is the code: function getFileContents(filePath, callbackFn) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { callbackFn(xhr.responseText); } }...

globals and locals in python exec()

Hi, I'm trying to run a piece of python code using exec. my_code = """ class A(object): pass print 'locals: %s' % locals() print 'A: %s' % A class B(object): a_ref = A """ global_env = {} local_env = {} my_code_AST = compile(my_code, "My Code", "exec") exec(my_code_AST, global_env, local_env) print local_env which results in ...

Accessing local variables after a $.post of datatype "script" has completed

I have two lists, one on the right and one on the left. When I click on a node on the left it does the following: 1) AJAX request $.post's the content of the node to the server 2) If the transaction is successful on the server, the node moves to the list on the right via jQuery To post to the server: $.post($(F).attr('...