let b = true;
const n = 1;
let s = "test";
let a = ["x", "y", "z", 5];
let o = {
a: 1,
b: 2,
"c d": 3,
[s]: 4
};
let x = null;
let a = true;
let b = 5;
let c = 0;
if (a && b) {
// Will be executed
}
let temp = a && b; // 5
let temp2 = c || b; // 5
Falsy: false, null, undefined, 0, NaN and ""
let sum = (a, b) => {
return a + b;
}
sum(2, 2); // 4
let list = [1, 2, 3];
let doubled = list
.map(element => element * 2);
// [2, 4, 6]
class MyClass extends MySuperclass {
constructor(name = "world") {
super();
this.name = name;
}
doStuff() {
return "Hello " + this.name;
}
static doOtherStuff() {
//...
}
}
let obj = new MyClass();
Everything is in a single thread
Shared with browser rendering
(*** queue of callbacks)
setTimeout(..., 0); goes to the end
Limited support for multi-threading via "Workers"
Almost like classic inheritance...
Class definition "prototype" is a shared object
(*** Prototype chain)
Beware of mutable objects on prototype
All prototypes are writable...
String.prototype.capitalize = () => {
return this.charAt(0).toUpperCase() +
this.substr(1);
}
Object.prototype.toXML = () => {
// Awesome stuff!
}
let getMappedData = () => {
return getDataFromServer()
.then(data => {
//...
return data.map(...);
})
.catch(error => {
//...
});
}
getMappedData()
.then(mappedData => {...});
let getMappedData = () => {
return getDataFromServer()
.then(data => {
//...
return data.map(...);
})
.catch(error => {
//...
});
}
getMappedData()
.then(mappedData => {...});
let gotcha = ("1" == 1); // true
let sensible = ("1" === 1); // false
let unexpected = parseInt("08"); // 10
let parsed = parseInt("08", 10); // 8
let wat = "abcdefg".substring(3, 2); // "c"
let normal = "abcdefg".substr(3, 2); // "de"
let ugly = () =>
{
return
{
data: "demo"
}
}
ugly(); // undefined
let gotcha = ("1" == 1); // true
let sensible = ("1" === 1); // false
let unexpected = parseInt("08"); // 10
let parsed = parseInt("08", 10); // 8
let wat = "abcdefg".substring(3, 2); // "c"
let normal = "abcdefg".substr(3, 2); // "de"
let ugly = () =>
{
return;
{
data: "demo"
}
}
ugly(); // undefined
export default class MyComponent extends React.Component {
render() {
return (
<div>
<p>Hello {this.props.name}</p>
<OtherComponent type="test"
name={this.props.name} />
</div>
);
}
}
import dynamicStuff from "./stuff/" + name;