JavaScript Deian Stefan (adopted from my & Edward Yangs CSE242 - - PowerPoint PPT Presentation

javascript
SMART_READER_LITE
LIVE PREVIEW

JavaScript Deian Stefan (adopted from my & Edward Yangs CSE242 - - PowerPoint PPT Presentation

JavaScript Deian Stefan (adopted from my & Edward Yangs CSE242 slides) Why JavaScript? Linga franca of the Internet Used in the browsers, used server-side, used for IoT Still evolving to address growing needs (EcmaScript)


slide-1
SLIDE 1

JavaScript

Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)

slide-2
SLIDE 2

Why JavaScript?

  • Linga franca of the Internet

➤ Used in the browsers, used server-side, used for IoT ➤ Still evolving to address growing needs (EcmaScript)

  • Interesting goals and design trade-offs
  • Illustrates many core concepts of CSE 130
slide-3
SLIDE 3

The great ideas [JavaScript]

Expressive power (say more with less) First-class functions Type inference Monads Pattern matching Exception handling Continuations Reliability and reuse Type polymorphism Modules Objects & inheritance Type classes Cross-cutting concerns Memory management Concurrency

slide-4
SLIDE 4

The great ideas [Haskell]

Expressive power (say more with less) First-class functions Type inference Monads Pattern matching Exception handling Continuations Reliability and reuse Type polymorphism Modules Objects & inheritance Type classes Cross-cutting concerns Memory management Concurrency

slide-5
SLIDE 5

The great ideas [C++]

Expressive power (say more with less) First-class functions Type inference Monads Pattern matching Exception handling Continuations Reliability and reuse Type polymorphism Modules Objects & inheritance Type classes Cross-cutting concerns Memory management Concurrency

slide-6
SLIDE 6

Today

  • A little bit of history
  • Concepts from JavaScript

➤ First-class functions ➤ Objects ➤ Language flexibility

slide-7
SLIDE 7

May 1995

slide-8
SLIDE 8

May 1995

We need a scripting language for the browser!

slide-9
SLIDE 9

May 1995

Can I use Scheme? We need a scripting language for the browser!

slide-10
SLIDE 10

May 1995

Can I use Scheme? We need a scripting language for the browser! Ha? No! Make it look like Java!

slide-11
SLIDE 11

One week later…

slide-12
SLIDE 12

One week later…

Here is a hacked up prototype!

slide-13
SLIDE 13

One week later…

Here is a hacked up prototype! Great! Let’s ship it!

slide-14
SLIDE 14

One week later…

Here is a hacked up prototype! Great! Let’s ship it! (It really took another year to embed it in the browser)

slide-15
SLIDE 15

JavaScript’s design goals [Eich, ICFP 2005]

  • Make it easy to copy/paste snippets of code

➤ Tolerate “minor” errors — e.g., missing semicolons

  • Simplify event handling (inspired by HyperCard)
  • Pick a few hard-working, powerful primitives

➤ First-class functions (based off Scheme/Lisp) ➤ Objects everywhere (based off Self/Smalltalk)

  • Leave all else out!
slide-16
SLIDE 16

JavaScript has evolved

  • EcmaScript 5 and 6 introduced many new features

➤ block scoping ➤ new types (Map, Set, Symbols, Uint8Array, etc.) ➤ strict mode ➤ module system ➤ classes

  • How could JavaScript have been useful without these?
slide-17
SLIDE 17

First-class functions!

slide-18
SLIDE 18

First-class functions

  • What does it mean for a language to have first class

functions? (functions are values)

slide-19
SLIDE 19

First-class functions

  • What does it mean for a language to have first class

functions? (functions are values)

➤ can be declared within any scope

slide-20
SLIDE 20

First-class functions

  • What does it mean for a language to have first class

functions? (functions are values)

➤ can be declared within any scope ➤ can be passed as arguments to a function

slide-21
SLIDE 21

First-class functions

  • What does it mean for a language to have first class

functions? (functions are values)

➤ can be declared within any scope ➤ can be passed as arguments to a function ➤ can be returned as result of function call

slide-22
SLIDE 22

Function as scoping primitive

  • Today: JavaScript has block scoping
  • But, until recently, JavaScript only had function-level

scoping

➤ What does this mean? ➤ How did people survive?

slide-23
SLIDE 23

scope-*.js

slide-24
SLIDE 24

Function as scoping primitive

  • Whenever you want a new scope:

➤ declare a new function ➤ immediately call it

  • Key requirement from language design:

➤ being able to declare function in any scope

slide-25
SLIDE 25

Okay! But…

  • Why do we want to pass functions as arguments?
  • Or return functions as results?
slide-26
SLIDE 26

Functions as args

  • Original reason: simple way to do event handling

➤ E.g., onclick(function() { alert(“button clicked!”); })

  • Still true today. But many other reasons, including:

➤ performance: asynchronous callbacks ➤ expressiveness: filter, map-reduce, etc.

slide-27
SLIDE 27

Performance?

  • Don’t need to block when reading file
  • Can tell runtime system to call your “callback” function
  • nce it’s read the file

➤ This allows runtime to schedule other IO concurrently

slide-28
SLIDE 28

perf-*.js

slide-29
SLIDE 29

Expressive power

  • Say more with less!

➤ E.g., filter all positive elements from array ➤ E.g., add 42 to every element of the array

  • In both cases: we are expressing the computation we

care about without telling the computer what to do

➤ Don’t need to clutter code with low-level

mechanisms!

➤ Opens up room for performance optimizations! How?

slide-30
SLIDE 30

expressive.js

slide-31
SLIDE 31

Why return functions?

  • With the other 2 properties: let’s you compose

functions from other functions

➤ Functions that do this are called “high-order”

  • E.g., function composition: (f ◦ g)(x) = f (g(x))

➤ Here ◦ is a function that takes 2 functions: f and g ➤ E.g., instead of map(map(list, f), g) we can do

map(list, g ◦ f): way faster!

slide-32
SLIDE 32

hof.js

slide-33
SLIDE 33

Aren’t these just function pointers?

slide-34
SLIDE 34

No! JavaScript functions are closures!

  • Closure = function code + environment

➤ Function pointers don’t keep track of environment ➤ We’ll see this in more detail in a few lectures

slide-35
SLIDE 35

closure.js

slide-36
SLIDE 36

What else can functions be used for?

  • EcmaScript now has notion of modules

➤ But most implementations still use functions

  • How can we use functions to implement modules?

➤ Closures are good for information hiding ➤ Locally declared variables are scoped to the function

(“module”)

➤ Function called with exports object which is used to

expose public variables/functions

slide-37
SLIDE 37

module*.js

slide-38
SLIDE 38

Today

  • A little bit of history ✓
  • Concepts from JavaScript ✓

➤ First-class functions ✓ ➤ Objects ➤ Language flexibility

slide-39
SLIDE 39

What are JavaScript Objects?

slide-40
SLIDE 40

What are JavaScript Objects?

  • Objects are maps of names (strings) to values
slide-41
SLIDE 41

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation:

slide-42
SLIDE 42

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation: ➤ e.g., const obj = { x: 3, y: “w00t” }

slide-43
SLIDE 43

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation: ➤ e.g., const obj = { x: 3, y: “w00t” } ➤ Properties are accessed with dot or bracket notation:

slide-44
SLIDE 44

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation: ➤ e.g., const obj = { x: 3, y: “w00t” } ➤ Properties are accessed with dot or bracket notation: ➤ e.g., obj.x or obj[“x”]

slide-45
SLIDE 45

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation: ➤ e.g., const obj = { x: 3, y: “w00t” } ➤ Properties are accessed with dot or bracket notation: ➤ e.g., obj.x or obj[“x”] ➤ Methods are function-valued properties

slide-46
SLIDE 46

What are JavaScript Objects?

  • Objects are maps of names (strings) to values

➤ E.g., object created with object literal notation: ➤ e.g., const obj = { x: 3, y: “w00t” } ➤ Properties are accessed with dot or bracket notation: ➤ e.g., obj.x or obj[“x”] ➤ Methods are function-valued properties ➤ e.g., obj.f = function (y) { return this.x + y; }

slide-47
SLIDE 47

What is “this”?

  • this is called the receiver

➤ Comes from Self (Smalltalk dialect) ➤ Will see more of this in objects lecture

  • Intuitively: this points to the object which has the

function as a method

➤ Really: this is bound when the function is called

slide-48
SLIDE 48

receiver.js

slide-49
SLIDE 49

I thought JavaScript had classes

  • Now it does! But it didn’t always
  • How did people program before?

➤ Used to use functions as constructors!

slide-50
SLIDE 50

What is a function constructor?

  • Just a function!

➤ When you call function with new the runtime binds

the this keyword to newly created object

➤ You can set properties on the receiver to populate

  • bject

➤ One property is special: prototype

slide-51
SLIDE 51

class.js

slide-52
SLIDE 52

Today

  • A little bit of history ✓
  • Concepts from JavaScript ✓

➤ First-class functions ✓ ➤ Objects ✓ ➤ Language flexibility

slide-53
SLIDE 53

Language flexibility

  • Does not require lines end in ‘;’

➤ Automatic ‘;’ insertion not always what you expect

  • Casts implicitly to avoid “failures”

➤ Useful in some case, usually source of errors

  • Hoisting

➤ Sometimes useful, but, variable declarations (though

not definitions) are also hoisted

slide-54
SLIDE 54

Language flexibility

  • Evaluate string as code with eval

➤ Need access to full scope at point of call ➤ Scope depends on whether call is direct or not

  • Can alter almost every object (“monkey patch”)

➤ Even built-in objects like window and fs ➤ What’s the problem with this?

slide-55
SLIDE 55

Takeaways

  • First-class functions are extremely powerful

➤ We’ll see this over and over

  • Language “flexibility” is not free

➤ Think about features before shipping them