JavaScript Concepts JavaScript is pretty hard to escape if you want - - PowerPoint PPT Presentation

javascript concepts
SMART_READER_LITE
LIVE PREVIEW

JavaScript Concepts JavaScript is pretty hard to escape if you want - - PowerPoint PPT Presentation

JavaScript Concepts JavaScript is pretty hard to escape if you want to do anything for the web C of the Internet Take JavaScript for instance. It's widely criticized in the POPL community for getting many things wrong. But it must have


slide-1
SLIDE 1

JavaScript Concepts

slide-2
SLIDE 2
slide-3
SLIDE 3

JavaScript is pretty hard to escape if you want to do anything for the web

C of the Internet

slide-4
SLIDE 4

– Nikhil Swamy, MS Research

“Take JavaScript for instance. It's widely criticized in the POPL community for getting many things wrong. But it must have gotten a ton of things right too,

  • therwise it wouldn’t be so popular. ”
slide-5
SLIDE 5

Javascript is: Dynamically Typed Object-Oriented-ish Functional-ish

slide-6
SLIDE 6
slide-7
SLIDE 7

Basically everything in JavaScript is an Object

slide-8
SLIDE 8

Why do people dislike JavaScript? Implicit Conversions, it’s hard to make JS crash Easily leads to strange behavior, unpredictible So you have to test your code a lot Weird behavior of builtins, == vs ===, etc… Javascript uses prototypical inheritance If you think about it like C++/Java, you will be terribly wrong

slide-9
SLIDE 9

Numbers, Strings, Booleans, null, and undefined Everything else is an object!

slide-10
SLIDE 10

The hardest thing to get your head around in JS is that

  • bjects don’t belong to a class per-se. Classes still exist,

but they’re more like recipes for objects

slide-11
SLIDE 11

{a: 23} {foo: 12, bar: (x) => x} {a: “hello”}

These are all objects in JS

Observations: JS “objects” are mostly dictionaries

slide-12
SLIDE 12

{ speed: 12, distance: 13 }

slide-13
SLIDE 13

x.speed

slide-14
SLIDE 14

x[“speed”]

slide-15
SLIDE 15

Write functions using the function keyword

function dist(x0, x1) { return Math.sqrt( (x1[0]-x0[0])**2 + (x1[1]-x0[1])**2); }

> dist [Function: dist]

slide-16
SLIDE 16

Like Racket, JavaScript has a fairly functional flavor to it…

slide-17
SLIDE 17

var x = function(x) { return x**2; } function twice(f) { return function(x) { return f(f(x)); }; }

slide-18
SLIDE 18

> [1,2,3].map(function (x) { return x.toString(); }) [ '1', '2', '3' ]

slide-19
SLIDE 19

> [1,2,3,4].reduce ((acc,next) => acc + " " + next.toString()); '1 2 3 4'

Another way to write functions in JS

slide-20
SLIDE 20

JavaScript has closures

function countUpFrom(x) { var counter = x; return function() { var cur = counter; counter = cur+1; return cur; } };

slide-21
SLIDE 21

> var startingAtFive = countUpFrom(5); undefined > startingAtFive() 5 > startingAtFive() 6 > startingAtFive() 7

slide-22
SLIDE 22

var myFirstCurry = function(word) { return function(user) { return [word , ", " , user].join(""); }; }; var HelloUser = myFirstCurry("Hello"); HelloUser("Aadhya"); // Output: "Hello, Aadhya”

Currying…

Q: What does this look like in Racket?

slide-23
SLIDE 23

Classes

slide-24
SLIDE 24

function Apple (typeofapple) { this.typeofapple = typeofapple; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.typeofapple + ' apple'; }; }

slide-25
SLIDE 25

function Apple (type) { this.type = type; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.type + ' apple'; }; }

Critical question: what gets passed in for this?

Observation: if I don’t explicitly specify, it goes to the default object

slide-26
SLIDE 26

new Apple(3)

function Apple (type) { this.type = type; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.type + ' apple'; }; }

Creates an empty object, let’s call it x Binds this to x Runs the Apple function using x as this

slide-27
SLIDE 27

new Apple(3)

function Apple (type) { this.type = type; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.type + ' apple'; }; }

{}

This:

Apple { type: 3, color: 'red', getInfo: [Function] }

slide-28
SLIDE 28

> new Apple Apple { type: undefined, color: 'red', getInfo: [Function] }

One really crummy thing about JS: it silently fails E.g., Apple needed an argument, but we didn’t pass it one. So JS just fills in undefined

slide-29
SLIDE 29

I can explicitly specify this by using the call builtin function

> var x = {}; > Apple.call(x) undefined > x { type: undefined, color: 'red', getInfo: [Function] }

Note: not idiomatic JS

slide-30
SLIDE 30

JS has a strange take on inheritance…

slide-31
SLIDE 31

var Car = { name: “plain old car” }

Object literal

slide-32
SLIDE 32

var car = { wheels: function() { return "I have " + this.numWheels(); } }

Object literal

Note: runtime error if I call wheels

slide-33
SLIDE 33

var mazda = { numWheels: function() { return 4; } };

var car = { wheels: function() { return "I have " + this.numWheels(); } }

slide-34
SLIDE 34

> mazda.__proto__ = car; { wheels: [Function: wheels] } > mazda { numWheels: [Function: numWheels] } > mazda.__proto__ { wheels: [Function: wheels] } > mazda.wheels() 'I have 4'

slide-35
SLIDE 35

mazda

When I want to look up wheels

mazda

Not here!!!

car

slide-36
SLIDE 36

mazda

When I want to look up wheels

Not here!!! Climb to prototype Found wheels here

function() { return "I have " + this.numWheels(); }

car

slide-37
SLIDE 37

mazda

When I want to look up wheels

car

Not here!!! Climb to prototype Found wheels here

function() { return "I have " + this.numWheels(); }

Now, this is mazda

mazda car

Need to lookup numWheels

Finds it here!

slide-38
SLIDE 38

Lookups are dynamic

car.wheels = function() { return “I have some number”; }

mazda.wheels() now gives “I have some number”

slide-39
SLIDE 39

Using __proto__ directly is terrible form Instead, use Object.create(car)

slide-40
SLIDE 40

Object.create(car)

Creates a new object Sets its prototype to be car Now all lookups go through car Unless you set otherwise, of course

slide-41
SLIDE 41

Object.create(car)

Creates a new object Sets its prototype to be car Now all lookups go through car Unless you set otherwise, of course

This effectively enables using car as a class

In the sense that a class is a blueprint for an object

slide-42
SLIDE 42

http://sporto.github.io/blog/2013/02/22/a-plain- english-guide-to-javascript-prototypes/ https://www.infoworld.com/article/3196070/node-js/10-javascript- concepts-nodejs-programmers-must-master.html

slide-43
SLIDE 43
slide-44
SLIDE 44

Why do people dislike JavaScript? Implicit Conversions, it’s hard to make JS crash Easily leads to strange behavior, unpredictible So you have to test your code a lot Weird behavior of builtins, == vs ===, etc… Javascript uses prototypical inheritance If you think about it like C++/Java, you will be terribly wrong