INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.

Slides:



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

Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Jeremy Wells
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Shortcuts for Lazy Programmers! Topics Increment and Decrement Operators Assignment Operators.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CS 2104 Prog. Lang. Concepts Subprograms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
CPS120: Introduction to Computer Science Functions.
Martin Kruliš by Martin Kruliš (v1.0)1.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Object Oriented Programming Elhanan Borenstein Lecture #5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
JavaScript scope and closures
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Functions What is a function –“sub” program, with an isolated set of statements –Accepts parameters, returns a result (perhaps) –Think of it as its own.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Classes (Part 1) Lecture 3
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
C Functions -Continue…-.
Modern JavaScript Develop And Design
Dr. Charles W. Kann III JavaScript Objects Dr. Charles W. Kann III
Function There are two types of Function User Defined Function
Programming Fundamentals Lecture #7 Functions
Lecture 4-7 Classes and Objects
Chapter 5 - Functions Outline 5.1 Introduction
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Dr. Bhargavi Dept of CS CHRIST
PZ09A - Activation records
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 341 Lecture 27 JavaScript scope and closures
Method of Classes Chapter 7, page 155 Lecture /4/6.
Scoping and Hoisting Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scope Rules.
Programming Methodology
Presentation transcript:

INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE

22 Outline Review JavaScript Programming Closure Anonymous functions Recursion Next Class JavaScript Objects

Closure A JavaScript function is an object. In JavaScript, a closure is created when a function is defined within another function (function nesting) 3

Closure The nested (inner) function is private to its containing (outer) function. The inner function can be accessed only from statements in the outer function. The inner function can use the arguments and variables of the outer function, The outer function cannot use the arguments and variables of the inner function. 4

Closure - Example function addSq(a, b) { var y = Number(prompt("Enter a number", "0")); return add(y); // return add(y + x); // error- x is not defined // outer function cannot access variables in inner func function add(x) { return sq(a)+ sq(b)+ sq(x); } function sq(n) { return n*n;} } alert(addSq(2,3)); sq(6); // error – sq is not defined // inner function is private to outer function 5

Multiply-nested functions function A(x) { B(2); function B(y) { C(3); function C(z) { alert(x + y + z); } } A(1); 6 B can access A C can access A & B => Scope chaining A cannot access B or C B cannot access C

Closure – Name Conflicts When two arguments or variables in the scopes of a closure have the same name, there is a name conflict. More inner scopes take precedence, so the inner- most scope takes the highest precedence, while the outer-most scope takes the lowest. 7

Example: function outside(y) { var x = 10; return inside(); function inside() { // var y = 100; return x+y; } alert( outside(20)); 8 Closure – Name Conflicts Display ? if remove //

Anonymous Function Dynamically declared at runtime. Not given a name. Are declared using the function operator instead of function declaration. E.g.: var sq = function(x) { return x*x; }; alert(sq(3)); var y = sq; alert(y(3)); 9

Closure & Anonymous function function sum(a) { var sum2 = function(x, y) { return a+ x+y; }; return sum2; } //alert (sum(100)(2,4)); // this line is equivalent to the next two lines var temp = sum(100); alert(temp(2,4)); 10

Why closures? Analogy to OOP. – A closure makes it possible to associate some data (the environment) with a function to operate on the data. – This is analogous to Object Oriented Programming (OOP), where we can associate some data (properties) to the object with one or more methods – The scoped variables in the inner function become private variables, which is the “Encapsulation” in Object Oriented Programming. 11

Why closures? Private methods. The mechanism of closures, (inner function can only be accessed/ invoked by its outer function), implements the same concept of private methods in other Object Oriented Programming (OOP) languages. Private methods provide powerful ways to manage the global namespace to keep the non-essential methods from cluttering up the public interface. 12

Why closures? Avoid global variables. Global variables are not reliable. They are not secure. They may conflict with other global variables in the same application which may cause your code failure and their code failure. And it is almost impossible to test it. 13

Why closures? Function factory. you can create more functions with the same function body definition and different environments 14 function makeAdder(x) { return function(y) { return x + y; }; } var add5 = makeAdder(5); var add10 = makeAdder(10); alert(add5(2)); // 7 alert(add10(2)); // 12

Recursive Functions A function that calls itself. function factorial(n) { return (n <= 1) ? 1 : n * factorial(n-1); } // recursion do { var x = Number(prompt("Enter a number (0 to stop):", “1”)); alert("The factorial of "+ x + " is: "+ factorial(x)); } while (x != 0); 15

Next Class JavaScript Objects 16

Thank you!