This file contains hidden or 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
// 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" |
This file contains hidden or 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
// 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" |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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); | |
} |
This file contains hidden or 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
// 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); |
This file contains hidden or 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
// 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 }) |
This file contains hidden or 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
// 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, |
This file contains hidden or 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
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 |
This file contains hidden or 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
/* | |
GLOBAL SCOPE | |
*/ | |
var global_var = "mango"; // global_var is accessible everywhwere | |
/* | |
Function scope is lexical | |
*/ | |
function otherFunction() { | |
var outer_var = "foo"; |
This file contains hidden or 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
// 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 |