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

16
JavaScript: The Good Parts vs. JavaScript: The Definitive Guide For next class, read http://eloquentjavascript.net/ chapters 1-4.

Transcript of JavaScript: The Good Parts vs. JavaScript: The Definitive...

Page 1: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

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

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

Page 2: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

C152 – Programming Language Paradigms Prof. Tom Austin, Fall 2014

JavaScript

Page 3: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

History of JavaScript

In 1995, Netscape hired Brendan Eich to implement Scheme within the web browser.

Brendan Eich After a few meetings, Scheme was deemed too weird…

In 10 days, he wrote the initial version of JavaScript (then called Mocha) for Netscape 2.0 Beta.

Page 4: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

JavaScript

•  JavaScript looks superficially similar to languages like C++ and Java

•  Primarily client-side programming (i.e. code running in your browser & your machine), but some server-side variants – JVM: Rhino & Nashorn – Node.js

•  http://w3schools.com/js/default.asp

Page 5: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

JavaScript is multi-paradigm:

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

Page 6: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Imperative JavaScript

function addList(list) { var i, sum=0;

for (i=0; i<list.length; i++){

sum += list[i];

}

return sum;

}

Page 7: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Functional JavaScript

var addList = function(list) { if (list.length === 0) {

return 0;

}

return list[0]

+ addList(list.slice(1));

}

Page 8: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

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);

Page 9: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Extended JavaScript Examples (in-class)

Page 10: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Introduction to Node.js

Page 11: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Node.js

•  A JavaScript runtime and library designed for use outside the browser, based off of Google's V8 engine

•  npm: package manager for Node.js •  http://nodejs.org/

Page 12: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

myFile.txt

This is my file. There are many like it,

but this one is mine.

Page 13: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

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

Page 14: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Resulting Output

all done This is my file.

There are many like it,

but this one is mine.

Page 15: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Synchronous File IO in Node

var data = fs.readFileSync( './myFile.txt');

console.log(data.toString()); console.log('all done');

Page 16: JavaScript: The Good Parts vs. JavaScript: The Definitive ...austin/cs152-fall14/slides/CS152-Day09-JavaScript.pdf · History of JavaScript In 1995, Netscape hired Brendan Eich to

Lab: Intro to JavaScript

In today's lab, you will explore both the functional and object-oriented aspects of JavaScript. See Canvas for details.