local-variables

Why a variable defined global is undefined? [duplicate]

最后都变了- 提交于 2019-11-26 11:09:40
问题 This question already has answers here : 'Hoisted' JavaScript Variables (3 answers) Closed 4 years ago . Hi guys here I have a simple function and a global variable. Why is myname undefined and not the string \"global\" ? var myname = \"global\"; // global variable function func() { alert(myname); // \"undefined\" var myname = \"local\"; alert(myname); // \"local\" } func(); Is not possible to refer to a outer variable that is define outside the scope of that function? and in this a global

Why do local variables require initialization, but fields do not?

孤街醉人 提交于 2019-11-26 06:58:50
问题 If I create a bool within my class, just something like bool check , it defaults to false. When I create the same bool within my method, bool check (instead of within the class), i get an error \"use of unassigned local variable check\". Why? 回答1: Yuval and David's answers are basically correct; summing up: Use of an unassigned local variable is a likely bug, and this can be detected by the compiler at low cost. Use of an unassigned field or array element is less likely a bug, and it is

Why is `a = a` `nil` in Ruby?

我的梦境 提交于 2019-11-26 05:59:01
问题 I watched this video. Why is a = a evaluated to nil if a is not defined? a = a # => nil b = c = q = c # => nil 回答1: Ruby interpreter initializes a local variable with nil when it sees an assignment to it. It initializes the local variable before it executes the assignment expression or even when the assignment is not reachable (as in the example below). This means your code initializes a with nil and then the expression a = nil will evaluate to the right hand value. a = 1 if false a.nil? # =>

Default Values and Initialization in Java

自作多情 提交于 2019-11-26 03:48:50
问题 Based on my reference, primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line System.out.println(a); will be an error pointing at the variable a that says variable a might not have been initialized whereas in the given reference, integer will have 0 as a default value. However, with the given code below, it will actually print 0 . public class Main { static int

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

*爱你&永不变心* 提交于 2019-11-26 03:08:36
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. But this code executes, and prints 1. Can anyone explain this behavior? Are all variables created in a module global/available to the entire module? Luke Maurer Python variables are scoped to the innermost function, class, or module in which they're assigned. Control

Returning string from C function

笑着哭i 提交于 2019-11-26 02:54:05
问题 I haven\'t used C in over 3 years, I\'m pretty rusty on a lot of things. I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this. Here is my code: #include <ncurses.h> char * getStr(int length) { char word[length]; for (int i = 0; i < length; i++) { word[i] = getch(); } word[i] = \'\\0\'; return word; } int main() { char wordd[10]; initscr(); *wordd = getStr(10); printw(\"The string is:\\n\"); printw(\"%s\\n\",

Access a function variable outside the function without using “global”

ぐ巨炮叔叔 提交于 2019-11-26 02:31:34
问题 I am trying to access a local function variable outside the function in Python. So, for example, bye = \'\' def hi(): global bye something something bye = 5 sigh = 10 hi() print bye The above works fine as it should. Since I want to find out if I can access bye outside hi() without using global bye , I tried: def hi(): something something bye = 5 sigh = 10 return hi() x = hi() print x.bye The above gives AttributeError: \'NoneType\' object has no attribute \'bye\' . Then, I tried: def hi():

What&#39;s the scope of a variable initialized in an if statement?

六眼飞鱼酱① 提交于 2019-11-26 01:38:59
问题 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. But this code executes, and prints 1. Can anyone explain this behavior? Are all variables created in a module global/available to the entire module? 回答1: Python

Can a local variable&#39;s memory be accessed outside its scope?

痞子三分冷 提交于 2019-11-25 23:55:09
问题 I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The output was 58 How can it be? Isn\'t the memory of a local variable inaccessible outside its function? 回答1: How can it be? Isn't the memory of a local variable inaccessible outside its function? You rent a hotel room. You put a book in the top drawer of the bedside table and go

In ArrayBlockingQueue, why copy final member field into local final variable?

泄露秘密 提交于 2019-11-25 22:45:30
问题 In ArrayBlockingQueue , all the methods that require the lock copy it to a local final variable before calling lock() . public boolean offer(E e) { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; lock.lock(); try { if (count == items.length) return false; else { insert(e); return true; } } finally { lock.unlock(); } } Is there any reason to copy this.lock to a local variable lock when the field this.lock is final ? Additionally, it also uses a local copy