- Objects in JavaScript specified with
{} - Objects describe key-value mappings with
key: value, - Keys can map to any kind of value
Number,String,Object,Function,Array, etc.
var car = {
gas: 30,
drive: function() {
console.log("Beep!");
},
name: "Ethan's Car",
}- The
thiskeyword describes the callee of a function - the object that invokes the function
var car = {
gas: 30,
drive: function(miles) {
this.gas = this.gas - miles / this.mpg;
},
mpg: 16,
name: "Ethan's Car",
}- JavaScript specifies functional scope
function a() {
var x = 3;
}
console.log(x); // x is undefinedthisis the caller of a function, always
var x = {
y: function() {
console.log(this);
},
z: 10
};
x.y();- What does
x.y()output?
> { y: [Function], z: 10 }- How about this one?
var x = {
y: function() {
console.log(this);
},
z: 10
};
var huh = x.y;
huh();> [Window global]-
JavaScript is growing, features are being added to the language
-
Arrow Functions
var sum = (a, b) => {
return a + b;
}
// or more succinctly:
var sum = (a, b) => a + b;class Car {
constructor(name) {
this.name = name;
this.gas = 30;
this.mpg = 15;
}
drive(miles) {
this.gas -= this.mpg / miles;
}
}- Object Destructuring
var x = { y: 3, z: "hello" };
var { y, z } = x;- Spread Operators
var arr = [1, 2, 3, 4];
var sum = (...args) => args.reduce((v, c) => v + c);- Template strings
var sayHappyBirthday = (name) => `Happy birthday, ${name}!`;letandconst- block scope
if (true) {
let x = 5;
}
console.log(x); // undefined- Asynchronous JavaScript and XML
- Used to request resources over HTTP, without a page reload
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
console.log(httpRequest.responseText);
}
};
request.open('GET', 'https://1crb6kfpove4.runkit.sh/status', true);
request.send();- An idea: send an AJAX request and place it into our page
var status = document.querySelector('#status');
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
status.innerHTML = request.responseText;
}
};- A better idea: send an AJAX request and generate elements that get placed into our page
function generateStatusBar(status) {
return "\
<div class='status'>\
<h1>" + status + "</h1>\
</div>\
";
}
$("body").append(
$(generateStatusBar(request.responseText))
);$is jQuery, a tool for making selecting DOM nodes and manipulating them way easier
- Managing a bunch of different
generateAnotherComponent(withParam1, withParam2)across different files (or one huge one), for every single dynamic component you need, is bad
- We need a way to create stateful components in an HTML page and render their data dynamically
- A result of the Facebook ads team - creating complex interfaces with lots of data entry is hard!
- A composable view library to manage self-contained components that consume a lot of data
<MyComponent
aProp="string value for a prop"
bProp={3} /* number value for a prop */
cProp={(a, b) => a + b} /* function value for a prop */
dProp={{ textAlign: 'left' }} /* object value for a prop */
>
{children}
</MyComponent>class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
/* Set a first step for state */
};
}
componentDidMount() {
/* Do some setup */
}
render() {
/* Return JSX or value */
return (<h1>{this.props.aProp}</h1>);
}
}constructor(): Initialize state and setup the component (before mounting)
componentDidMount(): Perform asynchronous actions after the component has mounted
render: Return one JSX element (possibly with children) that you want to displayrendershould be a pure function ofstateandprops
propsare passed down from a parent and can cause a component to update
stateis modified within a component instance and can cause a component to update
stateonly modified withthis.setState
Build a simple React app in CodePen that renders the weather in a user-specified city!