Skip to content

Instantly share code, notes, and snippets.

@hanmd82
Last active December 26, 2017 07:57
Show Gist options
  • Save hanmd82/f709cd2d220b21201388b711a7a64732 to your computer and use it in GitHub Desktop.
Save hanmd82/f709cd2d220b21201388b711a7a64732 to your computer and use it in GitHub Desktop.
ES6 katas for Block scope - http://es6katas.org/
// 7: block scope - let
describe('`let` restricts the scope of the variable to the current block', () => {
describe('`let` vs. `var`', () => {
it('`var` works as usual', () => {
if (true) {
var varX = true;
}
assert.equal(varX, true);
});
it('`let` restricts scope to inside the block', () => {
if (true) {
let letX = true;
}
assert.throws(() => console.log(letX));
});
});
describe('`let` usage', () => {
it('`let` use in `for` loops', () => {
let obj = {x: 1};
for (let key in obj) {}
assert.throws(() => console.log(key));
});
it('create artifical scope, using curly braces', () => {
{
let letX = true;
}
assert.throws(() => console.log(letX));
});
});
});
// 8: block scope - const
describe('`const` is like `let` plus read-only', () => {
describe('scalar values are read-only', () => {
it('number', () => {
const constNum = 0;
// constNum = 1;
assert.equal(constNum, 0);
});
it('string', () => {
const constString = 'I am a const';
// constString = 'Cant change you?';
assert.equal(constString, 'I am a const');
});
});
const notChangeable = 23;
it('const scope leaks too', () => {
assert.equal(notChangeable, 23);
});
describe('complex types are NOT fully read-only', () => {
it('array', () => {
const arr = [42, 23];
arr[0] = 42;
assert.equal(arr[0], 42);
});
it('object', () => {
const obj = {x: 1};
obj.x = 3;
assert.equal(obj.x, 3);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment