scope

In languages which create a new scope each time in a loop block, a new local copy of the local loop variable is created each time in that new scope?

It seems that in language like C, Java, and Ruby (as opposed to Javascript), a new scope is created for each iteration of a loop block, and the local variable defined for the loop is actually made into a local variable every single time and recorded in this new scope? For example, in Ruby: p RUBY_VERSION $foo = [] (1..5).each do |i| ...

In Javascript, a function starts a new scope, but we have to be careful that the function must be invoked so that the scope is created, is that so?

In Javascript, I am sometimes too immerged in the idea that a function creates a new scope, that sometimes I even think the following anonymous function will create a new scope when it is being defined and assigned to onclick: <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a> <a href="#" id="link3">ha link 3</a> ...

If the "with" statement in Javascript creates a new scope, why does the following code not work as expected? (the closure doesn't contain the new "x" in new scope each time)

If the with statement in Javascript creates a new scope, shouldn't clicking on the links show a different x which are in different scopes? It doesn't... <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a> <a href="#" id="link3">ha link 3</a> <a href="#" id="link4">ha link 4</a> <a href="#" id="link5">ha link 5</a>...

c++ function scope

I have a main function in A.cpp which has the following relevant two lines of code: B definition(input_file); definition.Print(); In B.h I have the following relevant lines of code: class B { public: // Constructors B(void); B(const char *filename); ~B(voi...

Scope quandary with namespaces, function templates, and static data

This scoping problem seems like the type of C++ quandary that Scott Meyers would have addressed in one of his Effective C++ books. I have a function, Analyze, that does some analysis on a range of data. The function is called from a few places with different types of iterators, so I have made it a template (and thus implemented it in a...

Validate HAML from ActiveRecord: scope/controller/helpers for link_to etc?

I like HAML. So much, in fact, that in my first Rails app, which is the usual blog/CMS thing, I want to render the body of my Page model using HAML (obviously I won't do the same for Comment!). So here is app/views/pages/_body.html.haml: .entry-content= Haml::Engine.new(body, :format => :html5).render(self) ...and it works (yay, recur...

What's the best way to resolve this scope problem?

I'm writing a program in python that uses genetic techniques to optimize expressions. Constructing and evaluating the expression tree is the time consumer as it can happen billions of times per run. So I thought I'd learn enough c++ to write it and then incorporate it in python using cython or ctypes. I've done some searching on st...

How can I access variables outside of current scope in javascript?

I'm writing an application in javascript and cannot figure it out how to access the variables declared in my function, inside this jquery parse. Inside I can access global variables, but I don't really want to create global vars for these values. Basically I want to extract file names from an xml document in the simulationFiles variable...

Using python decorator functions from a different module

I want to use a function from another module as a decorator, but I need it to manipulate the current module's global namespace. For example, I want to be able to go from this: class SomeClass: pass root = SomeClass to this: from othermodule import decorator @decorator class Someclass: pass Any ideas? ...

JS Anonymous Scope...

this Application.EventManager.on('Click', function(args) { // event listener, args is JSON TestAction.getContents(args.node.id, function(result, e) { console.log(result); this.add({ title: args.node.id, html: result }).show(); }); }); I'm really struggling with scope and anonymou...

Does any language (or debugging tool) have a build in function or method to print out the scope chains, so as to look at the different situations of what a scope chain contains?

Does any language or debug tool have a way to print out the scope chain for examination? Thanks. ...

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 to rewrite vs. upgrade?

All custom legacy software needs changing, or so say our users. Sometimes they want a feature or two added and all that is necessary to change a bit of code, add a control, or some other minor upgrade task. Sometimes they want to ditch their error-prone VB5 desktop solution and rewrite the whole thing as a rich Web 2.0 ASP.NET MVC applic...

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...

Associative Array / Object can't be read in functions

At the very beginning of the javascript file, I have: var lbp = {}; lbp.defaults = { minLength: 40 }; I can successfully alert it afterwards, with: alert(lbp.defaults.minLength); But as soon as I put it inside a function, when I alert, I get "Undefined". What gives, and how do I avoid this? Is it absolutely necessary to pass ...

Spring "session" scope of a bean

It seems to me that "session" scope is another means to keep objects in session as using setAttrubute / getAttribute Correct? You know, dont know why, it does not work for me. <bean id="sabreUser" class="util.MyUser" factory-method="getSomeUser" scope="session"> <const args...> What I see is that after the initialization and initi...

How to automatically set and attribute to controller

Maybe the question is not self-explanatory, so I will explain it through. The deal is: I got the variable $conn in the bootstrap class file. I'd like to make it global for every controller so that I just have to call $this->conn in the controller action scope in order to access the data inside. How would I do it? Thx ...

Difference between :: and -> in PHP

I always see people in serious projects use :: everywhere, and -> only occasionally in local environment. I only use -> myself and never end up in situations when I need a static value outside of a class. Am I a bad person? As I understand, the only situation when -> won't work is when I try following: class StaticDemo { privat...

In C++, what is the scope resolution ("order of precedence") for shadowed variable names?

In C++, what is the scope resolution ("order of precedence") for shadowed variable names? I can't seem to find a concise answer online. For example: #include <iostream> int shadowed = 1; struct Foo { Foo() : shadowed(2) {} void bar(int shadowed = 3) { std::cout << shadowed << std::endl; // What does t...

Spring bean's DESTROY-METHOD attribute and web-application "prototype"d bean

Can get work the attribute "destroy-method". First, even if I type non-existing method name into "destroy-method" attribute, Spring initialization completes fine (already strange!). Next, when a bean has a "prototype" scope, then I suppose it must be destroyed before the application is closed. That not happens, it is simply never ca...