Functions and Closures. JavaScript Closures Are Everywhere In JS we often want to say “when this thing happens, do something” event driven programming.

Slides:



Advertisements
Similar presentations
Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
JavaScript Lecture 6 Rachel A Ober
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
JavaScript davide morelli history LiveScript (1995) in Netscape java script is a misleading name started as a scripting counterpart.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Introduction to Programming (in JavaScript) in 10 minutes …hopefully Else.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Function the Ultimate Act III. Function Method Class Constructor Module.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
HTML JAVASCRIPT. CONTENTS Javascript Example NOSCRIPT Tag Advantages Summary Exercise.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
JavaScript scope and closures
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Creating Web Documents: JavaScript Ftp / file management: review Introduction to JavaScript Sources Homework: start review for midterm, work on Project.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
JavaScript for C# developers Dhananjay Microsoft MVP
Interpolation Variable Interpolation, Backslash Interpolation.
Let’s demonstrate some KRAD Action Components Kuali University: Apply Now Lab 3: Actions Lab Objectives Understand how to setup an action that invokes.
Lecture III. Example var a = 2; var b = 2; console.log(a + b);
Getting from Scripts to Applications Donald J. Sipe | Refresh Jacksonville | November 11 th 2008.
Expressions and Data Types Professor Robin Burke.
Click to add Text Javascript Presented by Bunlong VAN Basic.
Javascript Design Patterns. AMD & commonJS. RequireJS Marc Torrent Vernetta.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
Changing CSS Styles via JavaScript No readings?. 2 Review? You can change the CSS styling of an element with JavaScript Syntax is similar to accessing.
Build in Objects In JavaScript, almost "everything" is an object.
HTML & teh internets.
JavaScript: Functions
Scope, Objects, Strings, Numbers
JavaScript Functions.
Variable Scope Variable, variable, who’s got the variable?
Javascript Short Introduction By Thanawat Varintree,
Lecture III.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
HCI Lab 2.
Dynamic Scoping Lazy Evaluation
Functions, Regular expressions and Events
CS5220 Advanced Topics in Web Programming JavaScript Basics
CS5220 Advanced Topics in Web Programming Node.js Basics
CSE 341 Lecture 27 JavaScript scope and closures
Tutorial 10: Programming with javascript
CSE 341 Lecture 11 b closures; scoping rules
CISC101 Reminders Assignment 3 due today.
CS3220 Web and Internet Programming JavaScript Basics
Web Programming and Design
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Functions and Closures

JavaScript Closures Are Everywhere In JS we often want to say “when this thing happens, do something” event driven programming For example: When the user clicks on an image, show a bigger version of the image On clicking the image, show a new image styled a particular way. This is all done through closures

So what is a Closure? All functions are closures!

Lexical Closure Lexis means word (greek) “the words are bound/enclosed”

Function Definition function foo(bar) { return 1+1; } console.log(typeof foo);

Function Expression var foo = function (bar) { return 1+1; }; console.log(typeof foo);

Functions take parameters and return a value (an Object)

Notice the lack of difference between a function expression and definition. They’re the same!

Functions are just another type of object like String, Date, Array, Number, etc.

Let’s consider the implications

We can instantiate an object inside our function and return it function foo() { var d = new Date(); return d.toLocaleString(); } console.log(foo()); // this looks normal

We can instantiate an object inside our function and return it var foo = function() { var s = "and on earth peace to all people of good will"; return s; } console.log(foo()); // this looks normal

What happens if we replace a common object (like Date or String) with Function object?

We can instantiate an object inside our function and return it function foo() { //<-- the outer function var bar = function () { //<-- the inner function return "hello world from an inner function"; }; return bar; } var aFunction = foo(); console.log(aFunciton()); // this looks weird at first.

There are some interesting repercussions of this.

The inner function’s scope includes the scope of the outer function Feel free to read that a few times.

Reminder: What is Scope? When you can reference a variable

function scope as you’re used to it. var n = 1; function foo() { var n = 2; console.log("n === " n); } console.log("n === " + n); foo(); console.log("n === " + n);

function scope in functional languages var n = 1, inner; function foo() { var n = 2; console.log("n === ", n); return function () { console.log("inner n === " + n); }; } console.log("n === " + n); inner = foo(); inner(); console.log("n === " + n);

It also means this will print 1 each time var n = 1, inner; function foo() { console.log( " n === " + n); return function () { console.log( " inner n === " + n); }; } console.log( " n === " + n); inner = foo(); inner(); console.log( " n === " + n);

A Partial Summary Variables from outer functions are available to inner functions.

So what’s a global variable? A variable which forces itself from an inner scope into an outer.

Why do this? An example with setTimeout

setTimeout Takes a function and a time. Will call the function after that many seconds

Simple reminder script var reminder = prompt("What do you need to be reminded of?"); setTimeout(function () { alert(reminder) }, 10);