scope

Context and variable scope in ES6 loops and forEach

时光毁灭记忆、已成空白 提交于 2020-02-28 06:39:12
问题 In ES5, if I have to refer to the this context of parent function in child function I have to store it in a variable and access it inside child function using that variable. Something like this ... // variant 1 var self = this; this.nums.forEach(function (v) { if (v % 5 === 0) self.fives.push(v); }); ECMAScript has arrow functions so I can avoid this: // variant 2 this.nums.forEach((v) => { if (v % 5 === 0) this.fives.push(v) }) The question that I have is: If I was to declare a variable temp

Why can I declare a child variable with the same name as a variable in the parent scope?

纵然是瞬间 提交于 2020-02-23 09:57:49
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

JSLint and the “Expected to see a statement and instead saw a block.” error

丶灬走出姿态 提交于 2020-02-23 04:30:42
问题 I have picked up the habit of wrapping all of my case statements in curly brackets from programming in C because of this but JSLint is throwing a fit. It stops validating at that point. My question is: Is this such a bad practice in JS? Do I not have to worry about the scope issue because JS has function scope (I understand how that would be the case, I just want a good reason not to be 'consistent' on this)? (I know that different languages call for different practices, but i am trying to be

Lambda Scope Clarification

自闭症网瘾萝莉.ら 提交于 2020-02-19 08:34:37
问题 Why does my parameter x behave so erratically? Example 1 - Doesn't exist in the current context. Example 2 - Cannot reuse x because it's defined in a 'child' scope. Example 3 - Fine. This is the part where I am confused. Perhaps a different 'child' scope? Example 1 : List<int> list = new List<int> { 1, 2, 3, 4, 5 }; var result = list.Where(x => x < 3); Console.Write(result.ElementAt(x)); creates this compile time error: The name 'x' does not exist in the current context which I expect.

Lambda Scope Clarification

风流意气都作罢 提交于 2020-02-19 08:33:38
问题 Why does my parameter x behave so erratically? Example 1 - Doesn't exist in the current context. Example 2 - Cannot reuse x because it's defined in a 'child' scope. Example 3 - Fine. This is the part where I am confused. Perhaps a different 'child' scope? Example 1 : List<int> list = new List<int> { 1, 2, 3, 4, 5 }; var result = list.Where(x => x < 3); Console.Write(result.ElementAt(x)); creates this compile time error: The name 'x' does not exist in the current context which I expect.

Cmake global variable in macro scope

萝らか妹 提交于 2020-02-15 23:55:49
问题 I'm trying to create a global list and I want it appended in a macro. Here is my setup: project \__. CMakeLists.txt \__. level1 \__. CMakeLists.txt \__. level2a \__. CMakeLists.txt \__. level2b \__. CMakeLists.txt Here is my top level CMakeLists.txt : cmake_minimum_required(VERSION 2.8) macro(listappend var) list(APPEND MY_GLOBAL_LIST "${var}") message(STATUS "LIST IN MACRO SCOPE: ${MY_GLOBAL_LIST}") endmacro(listappend) set(MY_GLOBAL_LIST "") add_subdirectory(level1) message(STATUS "LIST: $

Why can I declare a child variable with the same name as a variable in the parent scope?

北战南征 提交于 2020-02-15 06:54:01
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

Why can I declare a child variable with the same name as a variable in the parent scope?

筅森魡賤 提交于 2020-02-15 06:52:30
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

Accessing global variable in css

隐身守侯 提交于 2020-02-05 03:36:53
问题 :root { --color: blue; } div { --color: green; color: var(--color) } #alert { --color: red; color: var(--color) } <p>What's my color?</p> <div>and me?</div> <div id='alert'> What's my color too? <p>color?</p> </div> In the above code, how can I access the global value of --color in div with id='alert'? Or in other words is there any way in CSS to access the global variable like the :: (scope resolution operator) in c++?? 回答1: You Can't do that with CSS. If You'll repeat the declaration of the

JavaScript new Function scope ReferenceError

耗尽温柔 提交于 2020-02-02 08:16:25
问题 is there any way to make the code below working? (function(){ var n = "abc"; (new Function("return alert(n);"))(); })(); If I run the code in browser result is: " Uncaught ReferenceError: n is not defined ". Also, I need to some other variables like "n" make accessible inside the "new Function" too. Please help, Thank you 回答1: So you need to make that variables global. (function(){ window.n = "abc"; (new Function("return alert(n);"))(); })(); 回答2: When you use the new Function method (which