Created
May 17, 2018 11:39
-
-
Save AKJAW/0040bfd70a31627def1cb416f0a951e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = console.log | |
//Primitive Immutability | |
// invalid, and also makes no sense | |
//2 = 2.5; | |
var x = 2; | |
x.length = 4; | |
x; // 2 | |
x.length; // undefined | |
var x = new Number( 2 ); | |
// works fine | |
x.length = 4; | |
var s = "hello"; | |
s[1]; // "e" | |
s[1] = "E"; | |
s.length = 10; | |
s; // "hello" | |
"use strict"; | |
var s = new String( "hello" ); | |
s[1] = "E"; // error | |
s.length = 10; // error | |
s[42] = "?"; // OK | |
//c(s); // String {"hello", 42: "?"} | |
//Value to Value | |
function addValue(arr) { | |
var newArr = [ ...arr, 4 ]; | |
return newArr; | |
} | |
addValue( [1,2,3] ); // [1,2,3,4] | |
function updateLastLogin(user) { | |
var newUserRecord = Object.assign( {}, user ); | |
newUserRecord.lastLogin = Date.now(); | |
return newUserRecord; | |
} | |
var user = { | |
lastLogin: 1 | |
}; | |
//c(user) //1 | |
//updateLastLogin( user ) | |
//c(user) //1 | |
//c(updateLastLogin( user )) //Date.now() | |
//Non-Local | |
function foo(arh) { | |
arh[0] = 5 | |
} | |
var arr = [1,2,3]; | |
foo( arr ); | |
//console.log( arr[0] ); //5 | |
//=> | |
arr = [1,2,3]; | |
foo( [...arr] ); // ha! a copy! | |
//console.log( arr[0] ); // 1 | |
var x = 0 | |
{ | |
let x = 1 | |
//c(x) //1 | |
} | |
//c(x) //0 | |
//Intent | |
var a = "420"; | |
// later | |
a = Number( a ); | |
// later | |
a = [ a ]; | |
//If after changing from "420" to 420, the original "420" value is no longer needed, | |
//then I think it's more readable to reassign a | |
//rather than come up with a new variable name like aNum. | |
//It's Freezing in Here | |
var x = Object.freeze( [ 2, 3, [4, 5] ] ); | |
// not allowed: | |
x[0] = 42; | |
// oops, still allowed: | |
x[2][0] = 42; | |
var arr = Object.freeze( [1,2,3] ); | |
foo( arr ); | |
//console.log( arr[0] ); // 1 | |
//Performance | |
/* | |
var state = Immutable.List.of( 4, 6, 1, 1 ); | |
var newState = state.set( 4, 2 ); | |
state === newState; // false | |
state.get( 2 ); // 1 | |
state.get( 4 ); // undefined | |
newState.get( 2 ); // 1 | |
newState.get( 4 ); // 2 | |
newState.toArray().slice( 2, 5 ); // [1,1,2] | |
*/ | |
// | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment