let

JavaScript: Understanding let scope inside for loop [duplicate]

我只是一个虾纸丫 提交于 2021-02-08 09:52:40
问题 This question already has answers here : Explanation of `let` and block scoping with for loops (3 answers) Closed last year . Please consider snippet below- for(let i = 1; i <= 5; i++) { setTimeout(function(){ console.log(i); },100); } In this case, logs inside setTimeout will contain values of variable i as per each iteration of the for loop, i.e the logs will be as follow 1 2 3 4 5 For this, I have read explanations over the Internet like - let creates a variable declaration for each loop

Stripping out let in Haskell

我的梦境 提交于 2021-02-06 19:57:09
问题 I should probably first mention that I'm pretty new to Haskell. Is there a particular reason to keep the let expression in Haskell? I know that Haskell got rid of the rec keyword that corresponds to the Y-combinator portion of a let statement that indicates it's recursive. Why didn't they get rid of the let statement altogether? If they did, statements will seem more iterative to some degree. For example, something like: let y = 1+2 z = 4+6 in y+z would just be: y = 1+2 z = 4+6 y+z Which is

Stripping out let in Haskell

余生长醉 提交于 2021-02-06 19:56:47
问题 I should probably first mention that I'm pretty new to Haskell. Is there a particular reason to keep the let expression in Haskell? I know that Haskell got rid of the rec keyword that corresponds to the Y-combinator portion of a let statement that indicates it's recursive. Why didn't they get rid of the let statement altogether? If they did, statements will seem more iterative to some degree. For example, something like: let y = 1+2 z = 4+6 in y+z would just be: y = 1+2 z = 4+6 y+z Which is

【SICP练习】5 练习1.9

ぃ、小莉子 提交于 2021-02-06 07:59:42
 以下是第一个加起两个正整数的方法,其中 inc 将参数加 1 , dec 将参数减 1 。 (define (+ a b) (if(= a 0) b (inc (+ (dec a) b)))) 用代换模型展示 (+ 4 5) 如下: (+ 4 5) (inc (+ 3 5)) (inc (inc (+ 2 5))) (inc (inc (inc (+ 1 5)))) (inc (inc (inc (inc (+ 0 5))))) (inc (inc (inc (inc 5)))) (inc (inc (inc 6))) (inc (inc 7)) (inc 8) 9 如上所示,在代换模型展示中包含了伸展和收缩两个阶段,并且伸展阶段所需的额外存储量和计算所需的步数都正比于参数 a 。因此这是一个线性递归过程。 以下是另一个加起两个正整数的方法。 (define (+ a b) (if(= a 0) b (+ (dec a) (inc b)))) 同样用代换模型展示 (+ 4 5) 如下: (+ 4 5) (+ 3 6) (+ 2 7) (+ 1 8) (+ 0 9) 9 在这个过程中并没有任何增长或者收缩,而其计算过程可用固定数目的状态变量( a )描述。这是一个线性迭代过程。 版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http:/

【SICP练习】2 练习1.6

こ雲淡風輕ζ 提交于 2021-02-06 07:47:19
 练习 1.6 这道题通过由一个新版本的 if 来引出,主要讨论的还是应用序和正则序的问题。我看到“将 if 提供为一种特殊形式”时还满头雾水,并不太清楚什么特殊形式。当再返回看 if 的语法时才发现,这在第 12 页 if 的一般表达式下面一段。如果 <predicate> 得到真值,解释器就去求值 <consequent> 并返回其值。注意,在此处已经返回其值了,并没有进行后续运算。 而通过 cond 写出来的常规过程的 if ,在解释器采用应用序求值的情况下,如果第一次运算 good-enough? 时为真,则直接返回了 guess 。 原文中的求平方根的程序: (define (new-if predicate then-clauseelse-clause) (cond(predicate then-clause) (elseelse-clause))) (define (sqrt-iter guess x) (new-if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define(sqrt-iter guess x) (if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 于是博主进行了如下测试: (sqrt

let Variable Scope

寵の児 提交于 2021-02-04 19:56:12
问题 The following code will output "1." However, shouldn't the keyword "let" not make x a global variable, thus making it invisible to das()? let is supposed to limit the scope of variables to only the block where they're declared, yet here, I'm seeing an inner function have access to a "let" variable, even though x was declared outside its scope. How is that possible? function letTest() { function das () { console.log(x); // How does this function have access to a let variable declared outside

let Variable Scope

我的梦境 提交于 2021-02-04 19:55:32
问题 The following code will output "1." However, shouldn't the keyword "let" not make x a global variable, thus making it invisible to das()? let is supposed to limit the scope of variables to only the block where they're declared, yet here, I'm seeing an inner function have access to a "let" variable, even though x was declared outside its scope. How is that possible? function letTest() { function das () { console.log(x); // How does this function have access to a let variable declared outside

Why does const work in some for-loops in JavaScript?

核能气质少年 提交于 2021-02-04 10:08:10
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)

Why does const work in some for-loops in JavaScript?

南楼画角 提交于 2021-02-04 10:04:03
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)

Creating Decode Path from JSON Data in Swift that Includes Numbers and Hyphens?

与世无争的帅哥 提交于 2021-01-29 09:32:13
问题 this is relatively new to me and I've searched high and low but have been unsuccessful in finding a similar scenario. I have retrieved some JSON Data from an API URL and have successfully decoded and output various values from this data as strings by parsing the data to a separate sheet and using structs and constants with the 'Decodable' value set. The problem I have is that one of the containers in the Json data is a hyphenated date in this format dates['2020-11-04'] so swift will not let