let

Variable scope + eval in Clojure

杀马特。学长 韩版系。学妹 提交于 2019-11-26 11:25:25
问题 In Clojure, (def x 3) (eval \'(prn x)) prints 3, whereas (let [y 3] (eval \'(prn y))) and (binding [z 3] (eval \'(prn z))) generate an \'Unable to resolve var\' exception. According to http://clojure.org/evaluation, eval , load-string , etc generate temporary namespaces to evaluate their contents. Therefore, I\'d expect neither of the above code samples to work, since (def x 3) is done in my current namespace, not the one created by eval . Why does the first code sample work and not the last

Confused by the difference between let and let* in Scheme

守給你的承諾、 提交于 2019-11-26 09:48:12
问题 Can anyone explain the difference simply? I don\'t think I understand the concept from the textbooks/sites I have consulted. 回答1: If you use let , you can't reference other bindings which appear in the same let expression. For example, this won't work: (let ((x 10) (y (+ x 6))) ; error! unbound identifier: x y) But if you use let* , it is possible to refer to previous bindings which appear in the same let* expression: (let* ((x 10) (y (+ x 6))) ; works fine y) => 16 It's all here in the

Why let and var bindings behave differently using setTimeout function? [duplicate]

和自甴很熟 提交于 2019-11-26 06:31:00
问题 This question already has answers here : What's the difference between using “let” and “var”? (33 answers) Explanation of `let` and block scoping with for loops (3 answers) Closed 4 years ago . This code logs 6 , 6 times: (function timer() { for (var i=0; i<=5; i++) { setTimeout(function clog() {console.log(i)}, i*1000); } })(); But this code... (function timer() { for (let i=0; i<=5; i++) { setTimeout(function clog() {console.log(i)}, i*1000); } })(); ... logs the following result: 0 1 2 3 4

Do let statements create properties on the global object?

血红的双手。 提交于 2019-11-25 23:58:54
问题 In JavaScript, var declarations create properties on the global object: var x = 15; console.log(window.x); // logs 15 in browser console.log(global.x); // logs 15 in Node.js ES6 introduces lexical scoping with let declarations that have block scope. let x = 15; { let x = 14; } console.log(x); // logs 15; However, do these declarations create properties on the global object? let x = 15; // what is this supposed to log in the browser according to ES6? console.log(window.x); // 15 in Firefox

What&#39;s the difference between using “let” and “var”?

我怕爱的太早我们不能终老 提交于 2019-11-25 22:51:21
问题 ECMAScript 6 introduced the let statement. I\'ve heard it that it\'s described as a \"local\" variable, but I\'m still not quite sure how it behaves differently than the var keyword. What are the differences? When should let be used over var ? 回答1: Scoping rules Main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block

What is the temporal dead zone?

纵饮孤独 提交于 2019-11-25 21:59:08
问题 I\'ve heard that accessing let and const values before they are initialized can cause a ReferenceError because of something called the temporal dead zone . What is the temporal dead zone, how does it relate to scope and hoisting, and in what situations is it encountered? 回答1: let and const have two broad differences from var : They are block scoped. Accessing a var before it is declared has the result undefined ; accessing a let or const before it is declared throws ReferenceError : console

Are variables declared with let or const not hoisted in ES6?

微笑、不失礼 提交于 2019-11-25 21:42:44
问题 I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected... console.log(typeof name); // undefined var name = \"John\"; ...variables declared with let or const seem to have some problems with hoisting: console.log(typeof name); // ReferenceError let name = \"John\"; and console.log(typeof name); // ReferenceError const name = \"John\"; Does this mean that variables declared with let or const are not hoisted? What is really going on