CS312: Programming Languages. Lecture 21: JavaScript

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

Spring JavaScript. John Mitchell Adapted by Mooly Sagiv. Reading: links on last slide Homework 1: 18/3 17/4

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

INF5750. Introduction to JavaScript and Node.js

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

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

Lecture 8 (7.5?): Javascript

CGS 3066: Spring 2015 JavaScript Reference

JavaScript for PHP Developers

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

Boot Camp JavaScript Sioux, March 31, 2011

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript: the Big Picture

JavaScript. The Bad Parts. Patrick Behr

Client-Side Web Technologies. JavaScript Part I

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JavaScript: Introduction, Types


Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

JavaScript Lecture 1

Web Application Development

JavaScript Programming

JavaScript: The Good Parts. Douglas Crockford Yahoo! Inc.

JavaScript: The Good Parts

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

CSC Web Programming. Introduction to JavaScript

JavaScript: More Syntax

CEU s (Continuing Education Units) 12 Hours (i.e. Mon Thurs 5 9PM or Sat Sun 8AM 5PM)

Functional JavaScript. Douglas Crockford Yahoo! Inc.

JavaScript: The Basics

Working with JavaScript

JavaScript: Sort of a Big Deal,

Client vs Server Scripting

Compilers. Prerequisites

JavaScript I Language Basics

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

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

CS 360 Programming Languages Interpreters

CSE 341: Programming Languages

Javascript. Many examples from Kyle Simpson: Scope and Closures

CS 5142 Scripting Languages

Such JavaScript Very Wow

A Structural Operational Semantics for JavaScript

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

2D Game design Intro to Html 5

Advanced Topics in Programming Languages Spring Semester, Lecture 2: March 13, JavaScript

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Notes on JavaScript (aka ECMAScript) and the DOM

CISC 1600 Lecture 2.4 Introduction to JavaScript

Ruby: Introduction, Basics

Programming language components

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

JavaScript Introduction

Ruby: Introduction, Basics

Programming. Syntax and Semantics

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

The JavaScript Language

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. JavaScript. Standard

JavaScript. What s wrong with JavaScript?

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Lecture 8: JavaScript

Intro. Scheme Basics. scm> 5 5. scm>

JavaScript: Coercion, Functions, Arrays

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

Module 5: Javascript, Cookies COM 420

Assigns a number to 110,000 letters/glyphs U+0041 is an A U+0062 is an a. U+00A9 is a copyright symbol U+0F03 is an

Current trends: Scripting (I) A bid part of interface design centers around dialogs

JavaScript Doesn't Have to Hurt: Techniques, Tools and Frameworks to Ease JavaScript Development

Ruby: Introduction, Basics

More Lambda Calculus and Intro to Type Systems

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Chapter 3 Data Types and Variables

Welcome to CS50 section! This is Week 10 :(

Chapter 1. Fundamentals of Higher Order Programming

Flow Control. CSC215 Lecture

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

Programming Language Basics

CS50 Quiz Review. November 13, 2017

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

JavaScript: Getting Started

Pace University. Fundamental Concepts of CS121 1

JSJS - Project Proposal

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

CS 11 Haskell track: lecture 1

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Concepts of Programming Languages

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

Lecture 16: Static Semantics Overview 1

16. Objects & JSON. Dr. Dave Parker. Informa;on and the Web, 2014/15

Operational Semantics. One-Slide Summary. Lecture Outline

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Session 6. JavaScript Part 1. Reading

Transcription:

CS312: Programming Languages Lecture 21: JavaScript Thomas Dillig Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 1/25

Why Discuss JavaScript? JavaScript is very widely used and growing Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 2/25

Why Discuss JavaScript? JavaScript is very widely used and growing Any AJAX application heavily relies on JavaScript Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 2/25

Why Discuss JavaScript? JavaScript is very widely used and growing Any AJAX application heavily relies on JavaScript JavaScript also has interesting language trade-offs Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 2/25

Why Discuss JavaScript? JavaScript is very widely used and growing Any AJAX application heavily relies on JavaScript JavaScript also has interesting language trade-offs You can think of JavaScipt as a hybrid language with features from almost everywhere glued together Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 2/25

JavaScript Target Every language has a design target: Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

JavaScript Target Every language has a design target: C: Systems programming Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

JavaScript Target Every language has a design target: C: Systems programming Java: Set-top box Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

JavaScript Target Every language has a design target: C: Systems programming Java: Set-top box JavaScript: Web scripting Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

JavaScript Target Every language has a design target: C: Systems programming Java: Set-top box JavaScript: Web scripting Every language modifies some abstract data structure Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

JavaScript Target Every language has a design target: C: Systems programming Java: Set-top box JavaScript: Web scripting Every language modifies some abstract data structure In JavaScipt, this is the document object model of an html web page Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 3/25

What s a Scripting Language? Answer: One language embedded in another Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Shell Scripts compute commands executed by the shell Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Shell Scripts compute commands executed by the shell Common characteristics of scripting languages: Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Shell Scripts compute commands executed by the shell Common characteristics of scripting languages: Lots of string support Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Shell Scripts compute commands executed by the shell Common characteristics of scripting languages: Lots of string support Simple structure with little/no declarations Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

What s a Scripting Language? Answer: One language embedded in another More specifically, a scripting language is used to write programs that produce inputs to another language processor Examples: Embedded JavaScript produces HTML to be displayed by the browser Shell Scripts compute commands executed by the shell Common characteristics of scripting languages: Lots of string support Simple structure with little/no declarations Flexibility preferred over efficiency, safety, common sense Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 4/25

JavaScript History Developed by Brendan Eich at Netscape in 1995 as scripting language for Navigator 2 Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 5/25

JavaScript History Developed by Brendan Eich at Netscape in 1995 as scripting language for Navigator 2 Later standardized for browser compatibility, called ECMAScript Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 5/25

JavaScript History Developed by Brendan Eich at Netscape in 1995 as scripting language for Navigator 2 Later standardized for browser compatibility, called ECMAScript Renamed to JavaScript in part of marketing deal with Sun - no relation to Java! Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 5/25

JavaScript History Developed by Brendan Eich at Netscape in 1995 as scripting language for Navigator 2 Later standardized for browser compatibility, called ECMAScript Renamed to JavaScript in part of marketing deal with Sun - no relation to Java! Today: Many implementations available Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 5/25

Motivation for JavaScript Netscape, 1995 Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Opportunity to define the HTML scripting language Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Opportunity to define the HTML scripting language Brendan Eich: I hacked the JS prototype in 1 week in May, and it showed! Mistakes were frozen early. Rest of the yer spend embedding in browser and cursing my design Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Opportunity to define the HTML scripting language Brendan Eich: I hacked the JS prototype in 1 week in May, and it showed! Mistakes were frozen early. Rest of the yer spend embedding in browser and cursing my design Initial uses of JavaScript: Form validation, page effects, dynamic content manipulation Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Opportunity to define the HTML scripting language Brendan Eich: I hacked the JS prototype in 1 week in May, and it showed! Mistakes were frozen early. Rest of the yer spend embedding in browser and cursing my design Initial uses of JavaScript: Form validation, page effects, dynamic content manipulation More recently: Web 2.0: Significant functionality implemented on web client Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

Motivation for JavaScript Netscape, 1995 Has >90% browser market share Opportunity to define the HTML scripting language Brendan Eich: I hacked the JS prototype in 1 week in May, and it showed! Mistakes were frozen early. Rest of the yer spend embedding in browser and cursing my design Initial uses of JavaScript: Form validation, page effects, dynamic content manipulation More recently: Web 2.0: Significant functionality implemented on web client Examples: Google Docs, Gmail, etc Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 6/25

JavaScript Design Goals Make it easy to copy/paste code Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 7/25

JavaScript Design Goals Make it easy to copy/paste code Tolerate minor errors (missing semicolon) Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 7/25

JavaScript Design Goals Make it easy to copy/paste code Tolerate minor errors (missing semicolon) Simplified even handling, e.g., onclick, onmousedown,... inspired by HyperCard Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 7/25

JavaScript Design Goals Make it easy to copy/paste code Tolerate minor errors (missing semicolon) Simplified even handling, e.g., onclick, onmousedown,... inspired by HyperCard Full features that make it easy to write and modify code that does something from all other languages Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 7/25

JavaScript Design Functions based on LISP/Scheme Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 8/25

JavaScript Design Functions based on LISP/Scheme We have higher order functions, lambda, etc Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 8/25

JavaScript Design Functions based on LISP/Scheme We have higher order functions, lambda, etc Objects in JavaScript are based on Smalltalk/Self var pt = {x: 10, move:function(dx){this.x+=dx}} Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 8/25

JavaScript Design Functions based on LISP/Scheme We have higher order functions, lambda, etc Objects in JavaScript are based on Smalltalk/Self var pt = {x: 10, move:function(dx){this.x+=dx}} But lots of issues Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 8/25

JavaScript Design Functions based on LISP/Scheme We have higher order functions, lambda, etc Objects in JavaScript are based on Smalltalk/Self var pt = {x: 10, move:function(dx){this.x+=dx}} But lots of issues Douglas Crockford: In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 8/25

Language Syntax JavaScript is case sensitive Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 9/25

Language Syntax JavaScript is case sensitive But HTML is not case sensitive, so any HTML object in JavaScript is also not Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 9/25

Language Syntax JavaScript is case sensitive But HTML is not case sensitive, so any HTML object in JavaScript is also not Example: onclick vs. ONCLICK Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 9/25

Language Syntax JavaScript is case sensitive But HTML is not case sensitive, so any HTML object in JavaScript is also not Example: onclick vs. ONCLICK Statements are terminated by returns or semi-colons Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 9/25

Language Syntax JavaScript is case sensitive But HTML is not case sensitive, so any HTML object in JavaScript is also not Example: onclick vs. ONCLICK Statements are terminated by returns or semi-colons JavaScript has blocks using { }, but no separate scope! Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 9/25

Variables You define variables using the var statement Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 10/25

Variables You define variables using the var statement But no declarations; variables are implicitly defined by their first use, which must be an assignment. Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 10/25

Variables You define variables using the var statement But no declarations; variables are implicitly defined by their first use, which must be an assignment. Note: Implicit definition has global scope, even if it occurs in nested scope { var x = "123" } return x; //will return "123" Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 10/25

Stand-alone JavaScript You can use the Rhino commend-line interpreter to play with JavaScript without a website Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 11/25

Stand-alone JavaScript You can use the Rhino commend-line interpreter to play with JavaScript without a website rhino has the same read-eval-print loop we have already seen in the LISP interpreter Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 11/25

Stand-alone JavaScript You can use the Rhino commend-line interpreter to play with JavaScript without a website rhino has the same read-eval-print loop we have already seen in the LISP interpreter Play with it! Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 11/25

JavaScript in the Browser Most of the time JavaScript is used in the browser to manipulate a web page Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 12/25

JavaScript in the Browser Most of the time JavaScript is used in the browser to manipulate a web page Main reason it is used: Only kind of program that anyone can run in any browser and expect to function Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 12/25

JavaScript in the Browser Most of the time JavaScript is used in the browser to manipulate a web page Main reason it is used: Only kind of program that anyone can run in any browser and expect to function This is the main reason JavaScript is popular Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 12/25

Web Example: Page Manipulation Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 13/25

Primitive Data Types Boolean: true and false Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: 64-bit floating point Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: 64-bit floating point No integer type! Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: 64-bit floating point No integer type! Special value NaN and Infinity Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: 64-bit floating point No integer type! Special value NaN and Infinity Strings using Unicode characters Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

Primitive Data Types Boolean: true and false Numbers: 64-bit floating point No integer type! Special value NaN and Infinity Strings using Unicode characters Special values null, undefined Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 14/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Objects by reference Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Objects by reference You can supply any number of arguments Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Objects by reference You can supply any number of arguments fun.length: number of arguments in definition Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Objects by reference You can supply any number of arguments fun.length: number of arguments in definition fun.arguments.length: number of arguments in call Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

JavaScript Functions Declarations can appear in function body, allowing for local variables and inner functions Parameter passing: Basic types by value Objects by reference You can supply any number of arguments fun.length: number of arguments in definition fun.arguments.length: number of arguments in call Anonymous (lambda) functions: (function (x,y) {return x+y}) (2,3); Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 15/25

Function Examples Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 16/25

Use of Anonymous Functions Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 17/25

Objects In JavaScript, an object is nothing but a collection of named properties Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 18/25

Objects In JavaScript, an object is nothing but a collection of named properties Can think of it almost like a hash table or associative array Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 18/25

Objects In JavaScript, an object is nothing but a collection of named properties Can think of it almost like a hash table or associative array Defined by a set of name:value pairs: objduck = { name:"quak", gender:"male" } Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 18/25

Objects In JavaScript, an object is nothing but a collection of named properties Can think of it almost like a hash table or associative array Defined by a set of name:value pairs: objduck = { name:"quak", gender:"male" } New properties can be added at any time: objduck.species = "mallard" Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 18/25

Objects In JavaScript, an object is nothing but a collection of named properties Can think of it almost like a hash table or associative array Defined by a set of name:value pairs: objduck = { name:"quak", gender:"male" } New properties can be added at any time: objduck.species = "mallard" Can have methods, can refer to this Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 18/25

Basic Object Features Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 19/25

Objects and this The this variable is a property of the activation object for a function call Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 20/25

Objects and this The this variable is a property of the activation object for a function call In most cases, this points to the object which has the function as property (or method) Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 20/25

Objects and this The this variable is a property of the activation object for a function call In most cases, this points to the object which has the function as property (or method) Example: var o = {x:10, f:function): o.f() {return this.x}} Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 20/25

Objects and this The this variable is a property of the activation object for a function call In most cases, this points to the object which has the function as property (or method) Example: var o = {x:10, f:function): o.f() {return this.x}} This will evaluate to 10 Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 20/25

JavaScript Functions and this Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 21/25

Local Variables stored in Scope Object Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 22/25

Concurrency JavaScript is single-threaded Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 23/25

Concurrency JavaScript is single-threaded However, AJAX model allows for some hacked-up asynchronous callback mechanism using XMLHttpRequest Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 23/25

Concurrency JavaScript is single-threaded However, AJAX model allows for some hacked-up asynchronous callback mechanism using XMLHttpRequest Widely used, but sad and pathetic hack Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 23/25

Concurrency JavaScript is single-threaded However, AJAX model allows for some hacked-up asynchronous callback mechanism using XMLHttpRequest Widely used, but sad and pathetic hack Another form of concurrency: Use SetTimeout for cooperative multitasking Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 23/25

Unusual features of JavaScript Built-in regular expressions Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 24/25

Unusual features of JavaScript Built-in regular expressions Add, delete methods of objects dynamically Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 24/25

Unusual features of JavaScript Built-in regular expressions Add, delete methods of objects dynamically Redefine native functions and objects Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 24/25

Unusual features of JavaScript Built-in regular expressions Add, delete methods of objects dynamically Redefine native functions and objects Iterate over methods of an object: for (variable in object) { statement } Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 24/25

JavaScript Overall JavaScript is in many ways the worst of all features combined in one language Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 25/25

JavaScript Overall JavaScript is in many ways the worst of all features combined in one language The language does everything possible to allow unreadable and buggy code Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 25/25

JavaScript Overall JavaScript is in many ways the worst of all features combined in one language The language does everything possible to allow unreadable and buggy code Dynamic features make an performant interpreter extremely difficult Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 25/25

JavaScript Overall JavaScript is in many ways the worst of all features combined in one language The language does everything possible to allow unreadable and buggy code Dynamic features make an performant interpreter extremely difficult And yet: JavaScript is one of the most widely-used languages! Thomas Dillig, CS312: Programming Languages Lecture 21: JavaScript 25/25