JavaScript: The Good Parts vs. JavaScript: The Definitive Guide For - - PowerPoint PPT Presentation

javascript the good parts vs javascript the definitive
SMART_READER_LITE
LIVE PREVIEW

JavaScript: The Good Parts vs. JavaScript: The Definitive Guide For - - PowerPoint PPT Presentation

JavaScript: The Good Parts vs. JavaScript: The Definitive Guide For next class, read http://eloquentjavascript.net/ chapters 1-4. CS 152: Programming Language Paradigms JavaScript Prof. Tom Austin San Jos State University History of


slide-1
SLIDE 1

JavaScript: The Good Parts vs. JavaScript: The Definitive Guide

For next class, read http://eloquentjavascript.net/ chapters 1-4.

slide-2
SLIDE 2

CS 152: Programming Language Paradigms

  • Prof. Tom Austin

San José State University

JavaScript

slide-3
SLIDE 3

History of JavaScript

1995: Netscape hired Brendan Eich.

Brendan Eich

After a few meetings, Scheme was deemed too weird… His job: implement Scheme for the web browser.

slide-4
SLIDE 4

In 10 days, Brendan Eich wrote the initial version of JavaScript for Netscape 2.0 Beta.

slide-5
SLIDE 5

JavaScript

  • Superficially similar to Java
  • Primarily client-side programming
  • Server-side variants:

–JVM: Rhino & Nashorn –Node.js

  • http://w3schools.com/js/default.asp
slide-6
SLIDE 6

JavaScript is multi-paradigm:

  • Imperative
  • Functional – "Scheme in C's clothing"
  • Object-oriented – Prototype-based
slide-7
SLIDE 7

Imperative JavaScript

function addList(list) { var i, sum=0; for (i=0; i<list.length; i++){ sum += list[i]; } return sum; }

slide-8
SLIDE 8

Functional JavaScript

var addList = function(list) { if (list.length === 0) { return 0; } return list[0] + addList(list.slice(1)); }

slide-9
SLIDE 9

Object-Oriented JavaScript

function Adder (amount) { this.amount = amount; } Adder.prototype.add = function(x){ return this.amount + x; } var myAdder = new Adder(1); var y = myAdder.add(7);

slide-10
SLIDE 10

Extended JavaScript Examples

(in-class)

slide-11
SLIDE 11

Introduction to Node.js

slide-12
SLIDE 12

Node.js

  • Server-side JavaScript
  • Based on Google's V8 engine
  • npm: Node.js package manager
  • http://nodejs.org/
slide-13
SLIDE 13

myFile.txt

This is my file. There are many like it, but this one is mine.

slide-14
SLIDE 14

File I/O in Node.js var fs = require('fs'); fs.readFile('myFile.txt', function(err,data) { if (err) throw err; console.log(""+data); }); console.log('all done');

Callback function

slide-15
SLIDE 15

Resulting Output

all done This is my file. There are many like it, but this one is mine.

slide-16
SLIDE 16

Synchronous File IO in Node var data = fs.readFileSync( './myFile.txt'); console.log(data.toString()); console.log('all done');

slide-17
SLIDE 17

Lab: Intro to JavaScript Today's lab explores both the functional and object-oriented aspects of JavaScript. See Canvas for details.