Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 05:51 (UTC -07:00)
View GitHub Profile
@StevenJL
StevenJL / react_class_arrow.jsx
Created December 1, 2017 22:06
react_class_arrow
// Without arrow functions
class MyComponent extends React.Component {
/*
We define a method on MyComponent objects, that
requires access to the MyComponent object as `this`.
*/
updateFoo() {
this.setState({
foo: "bar"
@StevenJL
StevenJL / modules.js
Created September 10, 2017 03:44
modules
// lib/math.js
export function sum(x, y) { return x + y; }
export var pi = 3.14159
// myApp.js
import {sum, pi} from "lib/math"
sum(pi, pi);
// otherApp.js
import * as math from "lib/math"
@StevenJL
StevenJL / destructuring.js
Created September 10, 2017 03:43
Destructuring
// Array Matching
var list = [1, 2, 3]
var [a, , b] = list
a // => 1
b // => 3
[b, a] = [a, b]
a // => 3
b // => 1
// Object Matching
@StevenJL
StevenJL / classes.js
Created September 10, 2017 03:42
classes
// Before ES6
function Person(name, age) {
this.name = name;
this.age = age;
}
// a greeter method on a Person object
Person.prototype.greet = function() {
console.log('Hi, I am ' + this.name);
}
@StevenJL
StevenJL / arrow2.js
Last active December 27, 2017 10:46
Arrow 2
// create a monkey constructor
function Monkey(favorite_food) {
this.favorite_food = favorite_food;
}
// set a method on the monkey constructor
Monkey.prototype.wait_and_ask = function() {
setTimeout(function () {
console.log('It has been 2 seconds, may I have a ' + this.favorite_food);
}, 2000);
@StevenJL
StevenJL / arrow.js
Created September 10, 2017 03:37
Arrow
// before ES6
var numbers = [1,2,3,4,5,6];
var even_numbers = numbers.filter(function(x) { return x%2 === 0 })
// with ES6
const numbers = [1,2,3,4,5,6];
const even_numbers = numbers.filter(x => x%2 === 0 })
@StevenJL
StevenJL / const.js
Created September 10, 2017 03:36
const
// cannot re-assign a const variable
const x = "written in stone";
x = "changed my mind"; // raises TypeError: Assignment to constant variable
// but you can still mutate a const variable
const person = {};
person.name = "Steve";
/*
`const` is great for assigning to a module that is required into code,
@StevenJL
StevenJL / let.js
Created September 10, 2017 03:33
let.js
if (false) {
var x = "never_assigned";
}
x //=> returns undefined instead of raising error
if (false) {
let x = "never_assigned";
}
x //=> raises a ReferenceError: x not defined
@StevenJL
StevenJL / javascript_scoping.js
Created September 10, 2017 03:31
javascript_scoping
/*
GLOBAL SCOPE
*/
var global_var = "mango"; // global_var is accessible everywhwere
/*
Function scope is lexical
*/
function otherFunction() {
var outer_var = "foo";
@StevenJL
StevenJL / c_notes_pointer_strings.c
Last active May 27, 2017 11:08
c_note_pointer_strings
// You can create a string as a character array
char some_text[] = "Here is some text";
// However, you cannot first instantiate a character array
// and then assign to a string.
char some_text[20];
some_text = "Here is some text"; // WRONG
// If you want to instantiate a variable first, and then
// assign to a string, you must use a pointer