JavaScript: Coercion, Functions, Arrays

Similar documents
COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

JavaScript: More Syntax

JavaScript: Objects, Methods, Prototypes

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

Ruby: Introduction, Basics

JavaScript: Sort of a Big Deal,

JavaScript Lecture 1

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Ruby: Introduction, Basics

Client-Side Web Technologies. JavaScript Part I

Review: Array Initializer Lists

Chapter 3 Data Types and Variables

Principles of Programming Languages

INF3110 Programming Languages Runtime Organization part II

INF5750. Introduction to JavaScript and Node.js

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JavaScript for C# Programmers Kevin

Static Analysis for JavaScript

CS 314 Principles of Programming Languages

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

JavaScript CS 4640 Programming Languages for Web Applications

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Web Application Development

CS312: Programming Languages. Lecture 21: JavaScript

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Ruby: Objects and Dynamic Types

Execution order. main()

Chapter 1. Fundamentals of Higher Order Programming

Software II: Principles of Programming Languages. Why Expressions?

JavaScript: Introduction, Types

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

CSC Web Programming. Introduction to JavaScript

INF3110 Programming Languages Runtime Organization part II

Variable Declarations

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Introduction to the R Language

Ruby: Introduction, Basics

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

Principles of Programming Languages

The Substitution Model

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Expressions and Assignment

JavaScript Lecture 2

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

HTML5 and CSS3 More JavaScript Page 1

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CHAPTER 7 OBJECTS AND CLASSES

Putting it all together: LINQ as an Example

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

Chapter 7. Expressions and Assignment Statements ISBN

CSc Introduction to Computing

CS 314 Principles of Programming Languages

Such JavaScript Very Wow

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

Chapter 7. Expressions and Assignment Statements

Introduction to Web Tech and Programming

Javascript Arrays, Object & Functions

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

JavaScript CS 4640 Programming Languages for Web Applications

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Functional JavaScript. Douglas Crockford Yahoo! Inc.

Introduction to Programming Using Java (98-388)

CS558 Programming Languages

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

egrapher Language Reference Manual

DaMPL. Language Reference Manual. Henrique Grando

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Sprite an animation manipulation language Language Reference Manual

A Brief Introduction to Scheme (II)

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

CS260 Intro to Java & Android 03.Java Language Basics

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

This document defines the ActionScript 3.0 language, which is designed to be forward- compatible with the next edition of ECMAScript (ECMA-262).

ECE570 Lecture 2: Types and Conventions

Operators and Expressions

ECE473 Lecture 2: Types and Conventions

CS 11 C track: lecture 6

Introduction to Functional Programming in Java 8

Decision Making in C

Timing for Interfaces and Abstract Classes

CS 5142 Scripting Languages

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

DLint: Dynamically Checking Bad Coding Practices in JavaScript

Ruby: Blocks, Hashes, and Symbols

The Substitution Model. Nate Foster Spring 2018

Transcription:

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

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

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

Conversion of Primitive Values string number boolean undefined undefined "undefined" NaN false null null "null" 0 false

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

== 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 consequences false == 'false' //=>false false == '0' //=>true ('0' == 0) && (0 == '') && ('0'!= '') dorey.github.io/javascript-equality-table

Functions are People too Named functions: declaration & use function foo(a, b) { foo("hi", 3); Anonymous functions function(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();

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

Functions Can Be Return Values function grantdegree() { function addtitle(name) { return "Dr. " + name; return addtitle; //a function! var phd = grantdegree(); phd("turing"); //phd is a function!

Closures function greaterthan(bound) { function compare (value) { return value > bound; return compare; //1-arg function var testpos = greaterthan(0); testpos(4) //=>true testpos(-3) //=>false

Closures + Anonymity function greaterthan(bound) { function compare (value) { return value > bound; return compare; //1-arg function var testpos = greaterthan(0); testpos(4) //=>true testpos(-3) //=>false

Closures + Anonymity function greaterthan(bound) { var compare = function(value) { return value > bound; return compare; //1-arg function var testpos = greaterthan(0); testpos(4) //=>true testpos(-3) //=>false

Closures + Anonymity function greaterthan(bound) { return function(value) { return value > bound; var testpos = greaterthan(0); testpos(4) //=>true testpos(-3) //=>false

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

Array Instantiation/Initialization Instantiate with new var 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 preferred var n = [10, 20, 30, 40]; var m = ["hi",, "world", 3.14]; [3, "hi", 17, [3, 4]].length == 4

Dynamic Size Arrays can grow var n = ["tree", 6, -2]; n.length == 3 //=>true n[8] = 17; n.length == 9 //=>true Arrays can shrink n.length = 2; //n is now ["tree", 6 ]

Arrays are Dynamic var n = [];

Arrays are Dynamic var n = []; n

Arrays are Dynamic n var n[0] = 4;

Arrays are Dynamic n 0 4

Arrays are Dynamic n 0 4 var n[3] = 3.14;

Arrays are Dynamic n 0 4 1 undefined 2 undefined 3 3.14

Arrays are Dynamic n 0 4 1 undefined 2 undefined 3 3.14 var n[1] = "hi";

Arrays are Dynamic n 0 1 4 "hi" 2 undefined 3 3.14

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