Today. Continue our very basic intro to JavaScript. Lambda calculus

Similar documents
Node.js Training JavaScript. Richard richardrodger.com

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

JavaScript: Sort of a Big Deal,

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

Lambda Calculus see notes on Lambda Calculus

Recap: Functions as first-class values

Client-Side Web Technologies. JavaScript Part I

The PCAT Programming Language Reference Manual

Reinventing the Enlightenment Object System

Organizing Code in Web Apps. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Building custom components IAT351

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

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

Classes vs Simple Object Delegation In JavaScript

Web Application Development

Swift by Practical Example. Justin Miller

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

SCHEME AND CALCULATOR 5b

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS558 Programming Languages

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Semantic Analysis. Lecture 9. February 7, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Operational Semantics. One-Slide Summary. Lecture Outline

(Refer Slide Time: 4:00)

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Object Model. Object Oriented Programming Spring 2015

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Dynamic Memory Allocation

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Introduction to Scheme

And Even More and More C++ Fundamentals of Computer Science

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

VARIABLES AND TYPES CITS1001

Outline Smalltalk Overview Pragmatic Smalltalk Closing. Pragmatic Smalltalk. David Chisnall. February 7,

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

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

More Lambda Calculus and Intro to Type Systems

Object-Oriented Principles and Practice / C++

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

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

CS312: Programming Languages. Lecture 21: JavaScript

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

CMSC 330: Organization of Programming Languages. Operational Semantics

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

QUIZ. What is wrong with this code that uses default arguments?

The SPL Programming Language Reference Manual

Functional Languages. Hwansoo Han

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

LECTURE 16. Functional Programming

CS Lecture #6. Administrative... Assignment #2 Graded NN Turned In, Average Grade: XX Assignment #3 due on Thursday

JavaScript: the Big Picture

Variable Declarations

Lesson 24 - Exploring Graphical Transformations and Composite Functions

Operational Semantics of Cool

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

Sprite an animation manipulation language Language Reference Manual

CS304 Object Oriented Programming Final Term

CS 360 Programming Languages Interpreters

C++ for Java Programmers

Variables and Bindings

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Fast Introduction to Object Oriented Programming and C++

JavaScript Patterns O'REILLY* S toy an Stefanov. Sebastopol. Cambridge. Tokyo. Beijing. Farnham K8ln

CSE 12 Abstract Syntax Trees

FP Foundations, Scheme

Object-Oriented Programming, Iouliia Skliarova

CS558 Programming Languages

Thursday, February 16, More C++ and root

Fundamentals and lambda calculus

Implementing Abstractions

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Ch. 11: References & the Copy-Constructor. - continued -

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

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson

VIRTUAL FUNCTIONS Chapter 10

COMP519 Web Programming Lecture 14: JavaScript (Part 5) Handouts

Weeks 6&7: Procedures and Parameter Passing

CMSC 330: Organization of Programming Languages

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

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Transcription:

JavaScript (cont)

Today Continue our very basic intro to JavaScript Lambda calculus

Last lecture recap JavaScript was designed in 10 days Lots of unsatisfactory parts (in retrospect); many due due to the design goals (more on this today) Got some things very right: first-class functions and objects First-class functions: what are the 3 properties? declaring functions in any scope: mimic block scoping funcs taking funcs as args: callbacks, filter, map

Why return functions? With the other 2 properties: let s you compose functions from other functions Functions that do this are called high-order E.g., function composition: (f g)(x) = f (g(x)) Here is a function that takes 2 functions: f and g E.g., instead of map(map(list, f), g) we can do map(list, g f): way faster!

Why return functions? With the other 2 properties: let s you compose functions from other functions Functions that do this are called high-order E.g., function composition: (f g)(x) = f (g(x)) Here is a function that takes 2 functions: f and g E.g., instead of map(map(list, f), g) we can do map(list, g f): way faster!

hof.js

Aren t these just function pointers?

No! JavaScript functions are closures! Closure = function code + environment Function pointers don t keep track of environment We ll see this in more detail in a few lectures

closure.js

What else can functions be used for? EcmaScript now has notion of modules But most implementations still use functions How can we use functions to implement modules? Closures are good for information hiding Locally declared variables are scoped to the function ( module ) Function called with exports object which is used to expose public variables/functions

module*.js

JavaScript intro A little bit of history Concepts from JavaScript First-class functions Objects Language flexibility

What are JavaScript Objects?

What are JavaScript Objects? Objects are maps of names (strings) to values

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation :

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation : e.g., const obj = { x: 3, y: w00t }

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation : e.g., const obj = { x: 3, y: w00t } Properties are accessed with dot or bracket notation:

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation : e.g., const obj = { x: 3, y: w00t } Properties are accessed with dot or bracket notation: e.g., obj.x or obj[ x ]

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation : e.g., const obj = { x: 3, y: w00t } Properties are accessed with dot or bracket notation: e.g., obj.x or obj[ x ] Methods are function-valued properties

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation : e.g., const obj = { x: 3, y: w00t } Properties are accessed with dot or bracket notation: e.g., obj.x or obj[ x ] Methods are function-valued properties e.g., obj.f = function (y) { return this.x + y; }

What is this? this is called the receiver Comes from Self (Smalltalk dialect) Will see more of this in objects lecture Intuitively: this points to the object which has the function as a method Really: this is bound when the function is called

receiver.js

I thought JavaScript had classes Now it does! But it didn t always How did people program before? Used to use functions as constructors!

What is a function constructor? Just a function! When you call function with new the runtime binds the this keyword to newly created object You can set properties on the receiver to populate object One property of the object is special: proto This is automatically set to the constructor prototype field (that s right! functions treated as objects)

class.js

Why are objects powerful? Useful for organizing programs Can hide details about the actual implementation and present clean interface that others can rely on I.e., they provide a way to build reliable software Enable reuse E.g., may want to add new kind of vehicle to the pipeline, can reuse lots of code that deals with assembling it E.g., in JavaScript an array is just an object!

Today A little bit of history Concepts from JavaScript First-class functions Objects Language flexibility

Language flexibility Does not require lines end in ; Automatic ; insertion not always what you expect Casts implicitly to avoid failures Useful in some case, usually source of errors (see notes) Hoisting Sometimes useful, but, variable declarations (though not definitions) are also hoisted

Language flexibility Evaluate string as code with eval Need access to full scope at point of call Scope depends on whether call is direct or not Can alter almost every object ( monkey patch ) Even built-in objects like window and fs What s the problem with this?

Takeaways First-class functions are extremely powerful We ll see this over and over Language flexibility is not free Think about features before shipping them!