let

Javascript ES6 'let' and 'var' - unexpected behavior inside function with argument name matching redeclared variable

元气小坏坏 提交于 2020-07-23 08:55:06
问题 Please note this is not duplicate of existing var vs let scope. I'm aware of the scope of var and let declarations and differences. But below scenario I could not justify with the understanding I had with let and var difference. In the below code, function foo accepts argument by name 'x' which has implicit let scope - as I cannot redeclare same variable name using let inside that function (uncommenting last line in function foo will throw JS error) "use strict"; function foo(x) { console.log

Javascript ES6 'let' and 'var' - unexpected behavior inside function with argument name matching redeclared variable

放肆的年华 提交于 2020-07-23 08:54:47
问题 Please note this is not duplicate of existing var vs let scope. I'm aware of the scope of var and let declarations and differences. But below scenario I could not justify with the understanding I had with let and var difference. In the below code, function foo accepts argument by name 'x' which has implicit let scope - as I cannot redeclare same variable name using let inside that function (uncommenting last line in function foo will throw JS error) "use strict"; function foo(x) { console.log

Javascript ES6 'let' and 'var' - unexpected behavior inside function with argument name matching redeclared variable

纵然是瞬间 提交于 2020-07-23 08:53:07
问题 Please note this is not duplicate of existing var vs let scope. I'm aware of the scope of var and let declarations and differences. But below scenario I could not justify with the understanding I had with let and var difference. In the below code, function foo accepts argument by name 'x' which has implicit let scope - as I cannot redeclare same variable name using let inside that function (uncommenting last line in function foo will throw JS error) "use strict"; function foo(x) { console.log

let vs var in javascript [duplicate]

扶醉桌前 提交于 2020-06-25 13:07:39
问题 This question already has answers here : Why let and var bindings behave differently using setTimeout function? [duplicate] (2 answers) Closed 2 years ago . I understand that let has block scope and var has functional scope. But I do not understand in this case, how using let will solve the problem const arr = [1,2,3,4]; for (var i = 0; i < arr.length; i++) { setTimeout(function() { console.log(arr[i]) }, 1000); } // Prints undefined 5 times const arr = [1,2,3,4]; for (let i = 0; i < arr

Difference between where bindings, let bindings and the single assignment operator (<-)

江枫思渺然 提交于 2020-05-24 21:22:27
问题 I do not understand the difference between the three syntaxes: where a = f (b) do a <- f (b) do let a = f (b) I do understand somehow though that a <- f(b) is different from the other two, in most cases where I tried all three worked. Also I read somewhere on the net that per block you should try to get along with one let binding only in order to be "idiomatic". But I never seem to manage. How do I decide what to use? 回答1: let foo = bar in ... simply defines foo to be the exact same thing as

private(set) with let properties - 'private(set)' modifier cannot be applied to read-only properties

邮差的信 提交于 2020-04-17 22:07:45
问题 I'm already aware of how private(set) works. But the below code is give compile-time error, class Person { private(set) let name: String //Error. private(set) let age: Int //Error. init(name: String, age: Int){ self.name = name self.age = age } } Error: 'private(set)' modifier cannot be applied to read-only properties Since name and age are not read-only properties, it shouldn't give such an error. If I use let instead of var , it is working fine. Just trying to know why? 回答1: private(set)

Is there no difference between let and const inside for of loop in Javascript in terms of declaration?

[亡魂溺海] 提交于 2020-04-14 08:29:49
问题 I recently came across this code: for (const temp of [1,2]) { // do something } I thought that it'd be better to use let declaration for temp because this way the variable would be declared only once. However, I also ran this example as well as the version with let through babel and this is what I see: for (const p of [1,2]) { } for (let s of [1,2]) { } became: for (var _i = 0, _arr = [1, 2]; _i < _arr.length; _i++) { var p = _arr[_i]; } for (var _i2 = 0, _arr2 = [1, 2]; _i2 < _arr2.length;

Is there no difference between let and const inside for of loop in Javascript in terms of declaration?

99封情书 提交于 2020-04-14 08:28:08
问题 I recently came across this code: for (const temp of [1,2]) { // do something } I thought that it'd be better to use let declaration for temp because this way the variable would be declared only once. However, I also ran this example as well as the version with let through babel and this is what I see: for (const p of [1,2]) { } for (let s of [1,2]) { } became: for (var _i = 0, _arr = [1, 2]; _i < _arr.length; _i++) { var p = _arr[_i]; } for (var _i2 = 0, _arr2 = [1, 2]; _i2 < _arr2.length;

JavaScript declare a variable and use the comma operator in one statement?

只谈情不闲聊 提交于 2020-04-11 05:45:25
问题 it's known that to declare multiple variables, one uses a format like: let k = 0, j = 5 /*etc....*/ It's also known that to execute multiple statements in one line (which is useful for arrow functions, making it not necessary to write the return keyword), the comma "," operator is also used, like so: let r = "hello there world, how are you?" .split("") .map(x => (x+=5000, x.split("").map( y => y+ + 8 ).join(""))) .join("") console.log(r) not the most elegant example, but the point is you can

How to simulate let expressions in JavaScript?

◇◆丶佛笑我妖孽 提交于 2020-03-03 08:43:06
问题 Consider the following implementation of take: const take = (n, [x, ...xs]) => n === 0 || x === undefined ? [] : [x, ...take(n - 1, xs)]; console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5] console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3] console.log(take(1, [undefined, 1])); // [] As you can see, it doesn't work for arrays with undefined because x === undefined is not the best way to test whether an array is empty. The following code fixes this problem: const take = (n, xs) => n ===