scoping

Javascript scoping of variables

不想你离开。 提交于 2019-12-04 07:19:59
问题 The output is 15 (in f, x is taken to be 10 and y is 7) with the following: var x = 5; function f(y) { return (x + y) - 2}; function g(h) { var x = 7; return h(x) }; { var x = 10; z = g(f); console.log(z) }; Why did x take the value from the 4th line and not from the 1st line (and why not the 3rd line)? 回答1: var s are not blocked scoped, so the last line is equivalent to x = 10; z = g(f); console.log(z) It should be clearer now that the value of x was changed to 10 before f was executed. It

Any pitfalls to using programmatically constructed formulas?

拜拜、爱过 提交于 2019-12-04 03:13:32
问题 I'm wanting to run through a long vector of potential explanatory variables, regressing a response variable on each in turn. Rather than paste together the model formula, I'm thinking of using reformulate() , as demonstrated here. The function fun() below seems to do the job, fitting the desired model. Notice, though, that it records in its call element the name of the constructed formula object rather than its value . ## (1) Function using programmatically constructed formula fun <- function

Scoping issue in Javascript

点点圈 提交于 2019-12-03 17:32:30
I need to have some information about the Scoping issue in Javascript. I know that it spports lexical(static) scoping, but, does not it support dynamic scoping as well? If you know anything about the scoping in Javascript, would you please share them with me ? Thanks I think you're confused because Javascript uses static scoping but at function-level, not at block level like usual structured languages. var foo = "old"; if (true) {var foo = "new";} alert (foo == "new") So be careful, blocks don't make scope! That's why you sometimes see loops with functions inside just to enable variables whose

What methods are there to modularize C code?

≡放荡痞女 提交于 2019-12-03 05:52:51
What methods, practices and conventions do you know of to modularize C code as a project grows in size? Create header files which contain ONLY what is necessary to use a module. In the corresponding .c file(s), make anything not meant to be visible outside (e.g. helper functions) static. Use prefixes on the names of everything externally visible to help avoid namespace collisions. (If a module spans multiple files, things become harder., as you may need to expose internal things and not be able hide them with "static") (If I were to try to improve C, one thing I would do is make "static" the

Scope functions apply/with/run/also/let: Where do the names come from?

爱⌒轻易说出口 提交于 2019-12-03 05:14:01
问题 There are quite a few blog posts (like this) on usages of the standard library functions apply / with / run / also / let available that make it a bit easier to distingish when to actually use which of those pretty functions. For a few weeks now, the official docs even provide guidelines on that topic finally: https://kotlinlang.org/docs/reference/coding-conventions.html#using-scope-functions-applywithrunalsolet Nevertheless, I think it is pretty hard to memorize the function's individual use

Common Lisp scoping (dynamic vs lexical)

天大地大妈咪最大 提交于 2019-12-02 19:37:45
EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below: Example 1: (defmethod fun1 (x) (print x) (fun2)) (defmethod fun2 () (print x)) (fun1 5) Output: 5 *** - EVAL: variable X has no value Question: This makes sense. x is statically scoped and fun2 has no way of

Scope functions apply/with/run/also/let: Where do the names come from?

感情迁移 提交于 2019-12-02 18:27:52
There are quite a few blog posts (like this ) on usages of the standard library functions apply / with / run / also / let available that make it a bit easier to distingish when to actually use which of those pretty functions. For a few weeks now, the official docs even provide guidelines on that topic finally: https://kotlinlang.org/docs/reference/coding-conventions.html#using-scope-functions-applywithrunalsolet Nevertheless, I think it is pretty hard to memorize the function's individual use cases by the function names . I mean, for me they seem to be interchangeable, why isn't let called run

Javascript scoping of variables

百般思念 提交于 2019-12-02 13:15:27
The output is 15 (in f, x is taken to be 10 and y is 7) with the following: var x = 5; function f(y) { return (x + y) - 2}; function g(h) { var x = 7; return h(x) }; { var x = 10; z = g(f); console.log(z) }; Why did x take the value from the 4th line and not from the 1st line (and why not the 3rd line)? Felix Kling var s are not blocked scoped, so the last line is equivalent to x = 10; z = g(f); console.log(z) It should be clearer now that the value of x was changed to 10 before f was executed. It is also important to note that free variables are evaluated when a function is called , not when

Python variable assigned by an outside module is accessible for printing but not for assignment in the target module

无人久伴 提交于 2019-12-02 04:40:19
I have two files, one is in the webroot, and another is a bootstrap located one folder above the web root (this is CGI programming by the way). The index file in the web root imports the bootstrap and assigns a variable to it, then calls a a function to initialize the application. Everything up to here works as expected. Now, in the bootstrap file I can print the variable, but when I try to assign a value to the variable an error is thrown. If you take away the assignment statement no errors are thrown. I'm really curious about how the scoping works in this situation. I can print the variable,

Javascript Odd Scoping Behavior

北战南征 提交于 2019-12-02 02:49:43
问题 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