scope

How to solve Var out of scope within setTimeout call

被刻印的时光 ゝ 提交于 2019-12-17 22:20:56
问题 I am trying to call a setTimeout from within a setInterval callback: function callback() { //assign myVar var myVar = document.getElementById("givenID"); //... //now wait 2 secs then call some code that uses myVAr setTimeout("myVar.innerHTML = 'TEST'", 2000); } setInterval("callback();", 10000); setInterval works as expected but setTimeout call is failing. I guess the problem is related to the fact that I am referencing a variable (myVar) that's not in scope. What's the best way to solve this

Auto Initialization of local variables

时间秒杀一切 提交于 2019-12-17 21:59:13
问题 I have the following code snippet. int j; printf("%d",j); As expected, I get a garbage value. 32039491 But when I include a loop in the above snippet, like int j; print("%d",j); while(j); I get the following output on multiple trials of the program. 0 I always thought local variables are initialized to a garbage value by default, but it looks like variables get auto initialized when a loop is used. 回答1: It is having indeterminate value . It can be anything. Quoting C11 §6.7.9 If an object

How to get the this of a object in a handler for a click event in jquery?

徘徊边缘 提交于 2019-12-17 21:19:27
问题 // begin signals this.loginSignal this.init = function(){ // init signals this.loginSignal = new t.store.helpers.Signal; // map events $('[value]="login"', this.node).click(this.login) } this.login = function(){ // this will dispatch an event that will be catched by the controller // but this is not refering to this class // and the next line fails :s this.loginSignal.dispatch(); } to make it work now i must add var $this = this; this line and use $this instead of this :S any clearer way

Is there a way to access the value of a local variable that has become hidden inside another scope?

本秂侑毒 提交于 2019-12-17 20:50:20
问题 I know if a variable is global, the you can always access its value by preceding the variable name with :: ... but is there a way to access the value of a local variable that has become hidden inside another scope? I thinking of something like this: void f() { int x = 1; { int x = 2; //access the value of the variable x (with the 1 in it) inside here } } If the language doesn't support this, then I'm perfectly okay with some hacky solution. 回答1: I think C++ doesn't support this. 回答2: You

Can I free() static and automatic variables in C?

ぃ、小莉子 提交于 2019-12-17 20:49:47
问题 The code is as follow : #include <stdlib.h> int num = 3; // Static external variable int *ptr = &num; int main(void) { int num2 = 4; // Automatic variable int *ptr2 = &num2; free(ptr); //Free static variable free(ptr2); //Free automatic variable return 0; } I try to compile the above code and it works, I'm curious does the free() function able to free both the static variable and also automatic variable? Or basically it does nothing? 回答1: Calling free() on a pointer not returned by memory

Assign and use of a variable in the same subshell

倖福魔咒の 提交于 2019-12-17 20:45:31
问题 I was doing something very simple like: v=5 echo "$v" and expected it to print 5 . However, it does not. The value that was just set is not available for the next command. I recently learnt that " In most shells, each command of a pipeline is executed in a separate SubShell ". However, in this case both commands are being executed in the same subshell. Why does this happen? Is there any way to have this working? Full example: $ v=1 $ v=5 echo "$v" 1 # I expected 5! 回答1: Let's look to the

Accessing class variables via instance

限于喜欢 提交于 2019-12-17 20:43:23
问题 In Python, class variables can be accessed via that class instance: >>> class A(object): ... x = 4 ... >>> a = A() >>> a.x 4 It's easy to show that a.x is really resolved to A.x , not copied to an instance during construction: >>> A.x = 5 >>> a.x 5 Despite the fact that this behavior is well known and widely used, I couldn't find any definitive documentation covering it. The closest I could find in Python docs was the section on classes: class MyClass: """A simple example class""" i = 12345

Child Scope & CS0136

爱⌒轻易说出口 提交于 2019-12-17 19:57:36
问题 The following code fails to compile stating "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else": var l = new List<string>(); l.Find(st => st.EndsWith("12")); string st = "why this fails?"; I understand why this won't work: string preParent = ""; { string preParent = "Should fail cause we change the meaning"; } When we do the following we get "CS0103: The name

If the “with” statement in Javascript creates a new scope, why does this closure doesn't contain the new “x” in new scope each time?

不羁的心 提交于 2019-12-17 19:36:48
问题 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> <script type="text/javascript"> for (i = 1; i <= 5; i++) { with({foo:"bar"}) { var x = i; document.getElementById('link' + i).onclick = function() { alert(x);

What's wrong with defining JavaScript variables within if blocks?

冷暖自知 提交于 2019-12-17 19:28:01
问题 I have some code like this: if (condition) { var variable = blah; } if (differentcondition) { var variable = blah; } Is this correct? I assume that the variable wouldn't be assigned if the condition doesn't return true. JSLint keeps on telling me, variable already defined. Am I doing this wrong? Thanks. OK, Here's my actual usecase, i'm doing event delegation like this: $("#container").click(function (event){ if ($(event.target).is('img.class1')) { var imagesrc = $(event.target).attr('src');