scoping

Javascript Odd Scoping Behavior

匆匆过客 提交于 2019-12-02 00:34:39
I've been going through Javascript function scope and have run into this: var scope = "global"; function f(){ console.log(scope); var scope = "local"; console.log(scope); } f(); Now I understand that the output of the first log is "undefined" because of how js hoists variables at the top of the function. BUT when I remove var from "var scope = "local";" the first log outputs "global" and this has got me scratching my head. Can someone explain why that is happening please? I mean doesn't js sequentially run the code? As such how can removing VAR have any impact on the first log? Two-pass

Why a variable can be accessible outside the loop in Python? [duplicate]

南笙酒味 提交于 2019-12-01 18:32:17
This question already has an answer here: Scoping in Python 'for' loops 6 answers Consider this example: for iter in xrange(10): myvar = iter print myvar # 9 Here myvar is clearly outside the loop? But it is still accessible. If this is Perl, it will throw an error. What's the reason behind such feature in Python? Is it harmful? What's the best practice then, to declare a variable before looping? There is no new scope created by the for loop (Ruby also behaves the same way). This may be surprising if you are coming from a language that creates new scopes for blocks. I don't believe it's

Julia 1.0 UndefVarError - Scope of Variable

给你一囗甜甜゛ 提交于 2019-12-01 10:33:41
问题 I am moving from Julia 0.7 to 1.0. It seems that Julia's rule for the scope of variables changed from 0.7 to 1.0. For example, I want to run a simple loop like this: num = 0 for i = 1:5 if i == 3 num = num + 1 end end print(num) In Julia 0.7 (and in most of other languages), we could expect num = 1 after the loop. However, it will incur UndefVarError: num not defined in Julia 1.0. I know that by using let I can do this let num = 0 for i = 1:5 if i == 3 num = num + 1 end end print(num) end It

Python scoping in dict comprehension

只谈情不闲聊 提交于 2019-12-01 03:37:20
>>> x = 'foo' >>> {0: locals().get('x')} {0: 'foo'} >>> {0: locals().get('x' + spam) for spam in ['']} {0: None} What is the reason for this discrepancy in behaviour? Dict comprehensions and generator comprehensions create their own local scope. List comprehensions do not in Python 2.x, but do in Python 3. (Note that your first example is not a dict comprehension. It's just a literal dict that happens to have an expression as the value for the key 0.) 来源: https://stackoverflow.com/questions/13117020/python-scoping-in-dict-comprehension

Scope of dot-dot-dot Arguments

早过忘川 提交于 2019-12-01 03:09:10
问题 I have a question on the scope of dot-dot-dot arguments. Consider the following function`foo = foo <- function(x, ...){ require(classInt); intvl = classIntervals(x, ...); return(intvl); } The function works great for the following calls x = runif(100, 0, 100); y1 = foo(x, n = 5, style = 'quantile'); y2 = foo(x, style = 'equal'); But when I try to use the style = 'fixed' argument, which needs a fixedBreaks argument as well, I get y3 = foo(x, style = 'fixed', fixedBreaks = seq(0, 100, 20))

Can I “extend” a closure-defined “class” in Javascript?

最后都变了- 提交于 2019-11-30 11:49:48
问题 I have a Javascript "class" defined like so: var Welcomer = function(name) { var pName = name; var pMessage = function() { return "Hi, " + pName + "!"; }; return { sayHi: function() { alert(pMessage()); } }; }; new Welcomer('Sue').sayHi(); Is there a way to "subclass" Welcomer in such a way that I can redefine the public methods and have access to the private methods and variables? The following will give me access to the public methods, but not to the private ones: var UnhappyWelcomer =

Can I use blocks to manage memory consumtion in C++?

岁酱吖の 提交于 2019-11-30 05:59:43
问题 I'm trying to gain some memory saving in a C++ program and I want to know if I can use blocks as a scope for variables (as in Perl). Let's say I have a huge object that performs some computations and gives a result, does it makes sense to do: InputType input; ResultType result; { // Block of code MyHugeObject mho; result = mho.superHeavyProcessing(); } /* My other code ... */ Can I expect the object to be destroyed when exiting the block? 回答1: Yes, you can. The destructor will be called as

Can I “extend” a closure-defined “class” in Javascript?

怎甘沉沦 提交于 2019-11-29 23:12:51
I have a Javascript "class" defined like so: var Welcomer = function(name) { var pName = name; var pMessage = function() { return "Hi, " + pName + "!"; }; return { sayHi: function() { alert(pMessage()); } }; }; new Welcomer('Sue').sayHi(); Is there a way to "subclass" Welcomer in such a way that I can redefine the public methods and have access to the private methods and variables? The following will give me access to the public methods, but not to the private ones: var UnhappyWelcomer = function(name) { var pSadMessage = function() { // won't work, b/c I don't have access to pMessage return

Verify object existence inside a function in R [duplicate]

牧云@^-^@ 提交于 2019-11-29 11:04:29
This question already has an answer here: How to check if object (variable) is defined in R? 6 answers I want to determine whether an object exists inside a function in R: foo <- function() { y <- "hello" if (exists(y, envir = sys.frame())) print(y) } foo() Error in exists(y, envir = sys.frame()) : invalid first argument I thought adding the envir = sys.frame() would do the trick. Also tried envir = environment() Expected foo() "hello" You should have checked ?exists : Usage: exists(x, where = -1, envir = , frame, mode = "any", inherits = TRUE) Arguments: x: a variable name (given as a

Scoping problem when sfApply is used within function (package snowfall - R)

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:53:26
Let me add another scoping problem in R, this time with the snowfall package. If I define a function in my global environment, and I try to use that one later in an sfApply() inside another function, my first function isn't found any more : #Runnable code. Don't forget to stop the cluster with sfStop() require(snowfall) sfInit(parallel=TRUE,cpus=3) func1 <- function(x){ y <- x+1 y } func2 <- function(x){ y <- sfApply(x,2,function(i) func1(i) ) y } y <- matrix(1:10,ncol=2) func2(y) sfStop() This gives : > func2(y) Error in checkForRemoteErrors(val) : 2 nodes produced errors; first error: could