Skip to content

Instantly share code, notes, and snippets.

@JoeShep
Created March 17, 2016 01:40
Show Gist options
  • Save JoeShep/7ce33a2730478f050316 to your computer and use it in GitHub Desktop.
Save JoeShep/7ce33a2730478f050316 to your computer and use it in GitHub Desktop.
"use strict";
function func1() {
if (true) {
let tmp = 123;
}
console.log("func1 tmp", tmp); // ReferenceError: tmp is not defined
}
func1();
// In contrast, var-declared variables are function-scoped:
function func2() {
if (true) {
var tmp = 123;
}
console.log("func2 tmp", tmp); // 123
}
func2();
// Block scoping means that you can shadow variables within a function:
function func3() {
var foo = 5;
if (true) {
var foo = 10;
}
console.log("func3 with var", foo);
}
func3();
function func4() {
let foo = 5;
if (true) {
let foo = 10; // shadows outer `foo`
console.log(foo); // 10
}
console.log("func4 4 with let", foo); // 5
}
func4();
const MAX_CAT_SIZE_KG = 3000;
MAX_CAT_SIZE_KG = 5000; // SyntaxError
console.log("MAX_Cat", MAX_CAT_SIZE_KG );
// ***************Arrow functions*******************
var reflect = function(value) {
return value;
};
console.log("reflect in ES5: ", reflect("ES5 is so yesterday.") );
// effectively equivalent to:
var reflect = value => value;
console.log("reflect in ES6: ", reflect("ES6 is the new hotness.") );
// With more than one arg you need parens
var sum = (num1, num2) => num1 + num2;
// effectively equivalent to:
var sum = function(num1, num2) {
return num1 + num2;
};
// With no args, you need an empty parens
var sum = () => 1 + 2;
// effectively equivalent to:
var sum = function() {
return 1 + 2;
};
// ******************Object Literal Property Value Shorthand****************
let wow = "Hi there",
es6 = "ES6",
myNum = function(){console.log("howdy");};
var myOldObj = {
wow: wow,
es6: es6,
myNum: myNum
}
// console.log("myOldObj", myOldObj );
var myNewObj = {
wow, es6, myNum
}
console.log("my New Obj", myNewObj.myNum );
myNewObj.myNum();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment