scope

Dealing with Scope in Object methods containing 'this' keyword called by Event Listeners

二次信任 提交于 2019-12-17 14:52:19
问题 I've generalized my lack of understanding of the situation to this small problem. Here's what I think I know so far: I have an object myDog (a global variable). Dog has a member variable el that is an html element; because it's an element, I can add event listeners to it. So, when you click on myDog.el , it logs to the console the values of this.name and myDog.name . As expected because of scope, this.name is undefined and myDog.name is 'tye'. this inside of Dog.speak when invoked by the

Variable types in CoffeeScript

核能气质少年 提交于 2019-12-17 13:42:44
问题 I'm not quite sure the uses for the different variables in CoffeeScript class Cow @utters = 1 constructor: (@name) -> mutate:-> alert @utters heads: 1 feet = 9 c = new Cow From my investigation, it seems heads is public and feet is private. My confusion comes in when figuring out name and utters . For name it more or less compiles to this.name = name and for utters it compiles to Cow.utters = 1 . So my questions are. What is the scope of utters and how should it be accessed? What is the scope

Variable types in CoffeeScript

你离开我真会死。 提交于 2019-12-17 13:41:23
问题 I'm not quite sure the uses for the different variables in CoffeeScript class Cow @utters = 1 constructor: (@name) -> mutate:-> alert @utters heads: 1 feet = 9 c = new Cow From my investigation, it seems heads is public and feet is private. My confusion comes in when figuring out name and utters . For name it more or less compiles to this.name = name and for utters it compiles to Cow.utters = 1 . So my questions are. What is the scope of utters and how should it be accessed? What is the scope

const reference to a temporary object becomes broken after function scope (life time)

南楼画角 提交于 2019-12-17 12:46:27
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . While asking this question, I learned const reference to a temporary object is valid in C++: int main () { int a = 21; int b = 21; //error: invalid initialization of non-const reference //int & sum = a + b;e [...] //OK int const & sum = a + b; return sum; } But in the following example, the const reference refnop refers to a destroyed temporary object. I

jquery - scope inside $(document).ready()?

丶灬走出姿态 提交于 2019-12-17 10:48:12
问题 So to stay organized, I have several javascript files, even though they all (in the end) are minified together to form one final javascript file. Each file's contents are wrapped in: $(document).ready(function(){ //some javascript here }); It seems like if I have things in separate files (in between that code) they don't have access to each other. Is this a scope issue? What can I do? For example, in one file I had a bunch of code to create tables from data received through ajax. However,

While without global

坚强是说给别人听的谎言 提交于 2019-12-17 10:09:29
问题 This snippet of code is from JuliaBoxTutorials myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"] i = 1; while i <= length(myfriends) friend = myfriends[i] println("Hi $friend, it's great to see you!") i += 1 end giving this error when run with Julia v1.0 UndefVarError: i not defined Stacktrace: [1] top-level scope at ./In[12]:5 [inlined] [2] top-level scope at ./none:0 But when i += 1 is replaced with global i += 1 it works. I guess this was still working in v0.6 and the tutorial

Accessing a static variable via an object reference in Java

泄露秘密 提交于 2019-12-17 09:56:36
问题 Why can we access a static variable via an object reference in Java, like the code below? public class Static { private static String x = "Static variable"; public String getX() { return this.x; // Case #1 } public static void main(String[] args) { Static member = new Static(); System.out.println(member.x); // Case #2 } } 回答1: Generally, public variables can be accessed by everybody, and private variables can only be accessed from within the current instance of the class. In your example you

variable scope in statement blocks

爷,独闯天下 提交于 2019-12-17 09:56:06
问题 for (int i = 0; i < 10; i++) { Foo(); } int i = 10; // error, 'i' already exists ---------------------------------------- for (int i = 0; i < 10; i++) { Foo(); } i = 10; // error, 'i' doesn't exist By my understanding of scope, the first example should be fine. The fact neither of them are allowed seems even more odd. Surely 'i' is either in scope or not. Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this? Or is just a case of

Local variable access to inner class needs to be declared final

馋奶兔 提交于 2019-12-17 09:49:35
问题 I got a problem of local variable access to inner class need to be declared final. It is from method createGrids() -> " squares[i][j] = 0; " that i is a local variable that need to be declared final. I don't know why and I have added final in fields but it is not working as well. import java.util.ArrayList; import java.util.Random; //omitted public class Minesweeper{ private JFrame frame; private int cols = 9; private int rows = 9; public static final int GRID_HEIGHT = 9; public static final

Is it better to declare a variable inside or outside a loop?

廉价感情. 提交于 2019-12-17 09:29:15
问题 Is better do: variable1Type foo; variable2Type baa; foreach(var val in list) { foo = new Foo( ... ); foo.x = FormatValue(val); baa = new Baa(); baa.main = foo; baa.Do(); } Or: foreach(var val in list) { variable1Type foo = new Foo( ... ); foo.x = FormatValue(val); variable2Type baa = new Baa(); baa.main = foo; baa.Do(); } The question is: What is faster 1 case or 2 case? Is the difference is insignificant? Is it the same in real applications? This may be a optimization-micro, but I really