25
Computer Science and Engineering College of Engineering The Ohio State University JavaScript: Coercion, Functions, Arrays Lecture 20

JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Embed Size (px)

Citation preview

Page 1: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering College of Engineering The Ohio State University

JavaScript:Coercion, Functions, Arrays

Lecture 20

Page 2: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Conversion of Primitive Values

String Number Boolean

numbers 0 "0" false

-0 "0" false

1 "1" true

NaN "NaN" false

Infinity "Infinity" true

-Infinity "-Infinity" true

6.022e23 "6.022e+24" true

Page 3: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Conversion of Primitive Values

string number boolean

boolean true "true" 1

false "false" 0

strings "" 0 false

"1.2" 1.2 true

"0" 0 true

"one" NaN true

Page 4: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Conversion of Primitive Values

string number boolean

undefined undefined "undefined" NaN false

null null "null" 0 false

Page 5: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Easier? Column-Major View

How do things convert to Boolean? Empty string is false Numbers (+/-)0 and NaN are false undefined and null are false

How do things convert to Numbers? Empty string is 0 Non-numeric strings are NaN undefined is NaN null is 0

Page 6: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

== Testing is… Different

When types don't match, coerce: null & undefined only equal each other Strings & booleans converted to numbers

'1' == true

Pitfall: NaN is not equal to NaN Object converted via valueOf or toString

Several surprising consequencesfalse == 'false' //=>falsefalse == '0' //=>true('0' == 0) && (0 == '') && ('0' != '')

dorey.github.io/JavaScript-Equality-Table

Page 7: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Functions are People too

Named functions: declaration & usefunction foo(a, b) { … }foo("hi", 3);

Anonymous functionsfunction(a, b) { … }//how do we invoke such a thing?

Functions are first-class citizens They can be assigned to variables!

var foo = function(a, b) {…};foo("hi", 3);var b = foo; //cf var b = foo();

Page 8: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Functions Can Be Arguments

function apply(x, a) { return x(a); //x is a function!

}

function square(i) { return i*i;

}

apply(square, 5) //=>25

Page 9: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Functions Can Be Return Valuesfunction grantDegree() {

function addTitle(name) { return "Dr. " + name;

} return addTitle; //a function!

}

var phd = grantDegree(); phd("Turing"); //phd is a function!

Page 10: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Closuresfunction greaterThan(bound) {

function compare (value) { return value > bound;

} return compare; //1-arg function

}

var testPos = greaterThan(0); testPos(4) //=>true testPos(-3) //=>false

Page 11: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Closures + Anonymityfunction greaterThan(bound) { function compare (value) {

return value > bound; } return compare; //1-arg function

}

var testPos = greaterThan(0); testPos(4) //=>true testPos(-3) //=>false

Page 12: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Closures + Anonymityfunction greaterThan(bound) { var compare = function(value) {

return value > bound; } return compare; //1-arg function

}

var testPos = greaterThan(0); testPos(4) //=>true testPos(-3) //=>false

Page 13: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Closures + Anonymityfunction greaterThan(bound) { return function(value) { return value > bound;

} }

var testPos = greaterThan(0); testPos(4) //=>true testPos(-3) //=>false

Page 14: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays: Basics

Numbered starting at 0 Indexed with [ ] Property length is # of elements

var sum = 0;for (var i = 0; i < n.length; i++) {

sum += n[i];}

Page 15: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Array Instantiation/Initialization

Instantiate with newvar n = new Array(3);

Initially, each element is undefined Note: Elements can be a mix of types

n[0] = 10;n[1] = "hi";n[2] = new Array(100);

Array literals usually preferredvar n = [10, 20, 30, 40];var m = ["hi", , "world", 3.14];[3, "hi", 17, [3, 4]].length == 4

Page 16: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Dynamic Size

Arrays can growvar n = ["tree", 6, -2];n.length == 3 //=>truen[8] = 17;n.length == 9 //=>true

Arrays can shrinkn.length = 2;//n is now ["tree", 6 ]

Page 17: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

var n = [];

Page 18: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

n

var n = [];

Page 19: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

n

var n[0] = 4;

Page 20: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

40n

Page 21: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

40n

var n[3] = 3.14;

Page 22: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

4

undefined

undefined

3.14

0

1

2

3

n

Page 23: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

4

undefined

undefined

3.14

0

1

2

3

n

var n[1] = "hi";

Page 24: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Arrays are Dynamic

4

"hi"

undefined

3.14

0

1

2

3

n

Page 25: JavaScript: Coercion, Functions, Arraysweb.cse.ohio-state.edu/~joseph.97/courses/3901/lectures/lecture20.pdf · dorey.github.io/JavaScript-Equality-Table. Computer Science and Engineering

Computer Science and Engineering The Ohio State University

Summary

Functions as first-class citizens Can be passed as arguments Can be returned as return values! Closure: carry their context

Arrays are fun too Dynamically sized Shorthand syntax