CSE331�Lecture 11.2
JavaScript Wrap up
Ardi Madadi �Summer 2021�(Based on slides by Kevin Zatloukal, Mike Ernst, Dan Grossman, and many others)
1
Problems
These tools can be used to write Gmail
But it has a number of problems…
CSE 331 Summer 2021
TypeScript
let x : number = 0;
quarter: string;
CSE 331 Summer 2021
TypeScript
CSE 331 Summer 2021
TypeScript Types
CSE 331 Spring 2021
TypeScript Example
register-ts/…
CSE 331 Spring 2021
TypeScript
CSE 331 Spring 2021
Classes and Event Listeners
let obj = {f: (x) => x + 1};
console.log(obj.f(2)); // 3
CSE 331 Summer 2021
this
let obj = {
a: 3,
f: function (x) { return x + this.a }
};
console.log(obj.f(2)); // 5
CSE 331 Summer 2021
this
let obj = {
a: 3,
f: function (x) { return x + this.a; }
};
console.log(obj.f.call(obj, 2)); // 5
console.log(obj.f.call({a:4}, 2); // 6
CSE 331 Summer 2021
this
function compute(f) {
return f(2); // no “obj.” so no “this”
}
let obj = {
a: 3,
f: function (x) { return x + this.a; }
};
console.log(compute(obj.f));
CSE 331 Summer 2021
// NaN!
Why should I care about this?
btn.addEventListener(“click”, obj.foo)
CSE 331 Summer 2021
How to fix this
btn.addEventListener(“click”,
foo.bind(obj))
btn.addEventListener(“click”,
evt => this.onClick(evt))
CSE 331 Summer 2021
Problems
This is better, but it still has problems…
CSE 331 Spring 2021
JSX
let x = <p>Hi, {name}.</p>;
CSE 331 Spring 2021
JSX Gotchas
CSE 331 Spring 2021
Problems
This is even better, but it still has problems…
CSE 331 Spring 2021
React
let app = (
<div>
<TitleBar name=“My App”/>
<EditPane rows=“80” />
</div>);
CSE 331 Spring 2021
React
class TitleBar extends React.Component {
CSE 331 Spring 2021
React Example
register-react/…
CSE 331 Spring 2021
React State
CSE 331 Spring 2021
Structure of a React Application
CSE 331 Spring 2021
Model
Listeners
HTML
data and invariants
presentation
events
updates
React State
CSE 331 Spring 2021
Example 5
register-react2/…
CSE 331 Spring 2021
Event Listeners
CSE 331 Spring 2021
Structure of Example React App
Quarter Picker
App
Class
Picker
State:
– quarter
onPick
Props:
– quarter
State:
– classes
quarter
onBack
React State
CSE 331 Spring 2021
Splitting the Model
CSE 331 Spring 2021
Structure of a React Application
CSE 331 Spring 2021
Model
Listeners
HTML
data and invariants
presentation
events
updates
React Components
Structure of a React Application
<input type=“text” value={…}>
CSE 331 Spring 2021
React setState
// this.state.x is 2
this.setState({x: 3});
console.log(this.state.x); // still 2!
CSE 331 Spring 2021
React Gotchas
CSE 331 Spring 2021
React Performance
CSE 331 Spring 2021
React Tools
CSE 331 Spring 2021