Lecture III. Example var a = 2; var b = 2; console.log(a + b);

Slides:



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

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.
Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
NaN (Not a Number) 1.Result of erroneous operations. 2.Any arithmetic operation with NaN as an input will have NaN as a result. 3.NaN is not equal to anything,
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
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.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
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
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Taking JavaScript Seriously IS NOT THE WORST IDEA.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Martin Kruliš by Martin Kruliš (v1.0)1.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Functions and Closures. JavaScript Closures Are Everywhere In JS we often want to say “when this thing happens, do something” event driven programming.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CSE 341 Lecture 29 a JavaScript, the bad parts slides created by Marty Stepp see also: JavaScript: The Good Parts, by.
JavaScript, Fourth Edition
Function the Ultimate Act III. Function Method Class Constructor Module.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
JavaScript scope and closures
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MTA EXAM HTML5 Application Development Fundamentals.
JavaScript for C# developers Dhananjay Microsoft MVP
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Expressions and Data Types Professor Robin Burke.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Click to add Text Javascript Presented by Bunlong VAN Basic.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Week 5 Part 2 Kyle Dewey. Overview Scope Lifetime Testing Exam #1 overview.
Advanced Programming in C
CGS 3066: Web Programming and Design Spring 2017
Chapter 4 Repetition Statements (loops)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Fundamentals
Functions Declarations, Function Expressions and IIFEs
CS106A, Stanford University
Chapter 19 JavaScript.
CS106A, Stanford University
Scope of Variables.
Lecture III.
CSC215 Lecture Flow Control.
CS5220 Advanced Topics in Web Programming JavaScript Basics
Chapter 8: More on the Repetition Structure
CS5220 Advanced Topics in Web Programming Node.js Basics
CS3220 Web and Internet Programming JavaScript Basics
CSE 341 Lecture 27 JavaScript scope and closures
JavaScript CS 4640 Programming Languages for Web Applications
The <script> Tag
CS3220 Web and Internet Programming JavaScript Basics
CGS 3066: Web Programming and Design Spring 2016
Topics to cover Instance variables vs. local variables.
Programming II Vocabulary Review Loops & functions
Scope Rules.
CISC 110 Day 2 Programming Basics.
Presentation transcript:

Lecture III

Example var a = 2; var b = 2; console.log(a + b);

Recap of the previous lecture

Scope in ES3 World

The Scope of a Variable The scope of a variable are the locations where it is accessible. For example: function foo() { var x; }

Scope - is a logical boundaries in which a variable (or expression) has its meaning. For example, a global variable, a local variable, etc, which generally reflects a logical range of a variable lifetime.

Scope - is a logical boundaries in which a variable (or expression) has its meaning. For example, a global variable, a local variable, etc, which generally reflects a logical range of a variable lifetime.

Scope can be nested function f() { var x = 2; function g() { var y = 3; alert(x + y); }

Nested Scope function f() { var x = 2; function g() { var y = 3; alert(x + y); } inner scope outer scope

Shadowing var scope = "global "; // A global variable function outer() { var scope = “outer”; // A outer variable function inner() { var scope = “inner"; document.write(scope); // Prints "inner" } inner(); } outer(); var scope = "global "; // A global variable function outer() { var scope = “outer”; // A outer variable function inner() { var scope = “inner"; document.write(scope); // Prints "inner" } inner(); } outer();

Shadowing var scope = "global "; // A global variable function outer() { var scope = “outer”; // A outer variable function inner() { document.write(scope); // Prints "outer" } inner(); } outer(); var scope = "global "; // A global variable function outer() { var scope = “outer”; // A outer variable function inner() { document.write(scope); // Prints "outer" } inner(); } outer();

Shadowing var scope = "global "; // A global variable function outer() { function inner() { document.write(scope); // Prints "global" } inner(); } outer(); var scope = "global "; // A global variable function outer() { function inner() { document.write(scope); // Prints "global" } inner(); } outer();

function X() { var a = 3, b = 5; function foo () { var b = 7, c = 11; a += b + c; }; foo(); alert("a = " + a + "; b = " + b); } X(); function X() { var a = 3, b = 5; function foo () { var b = 7, c = 11; a += b + c; }; foo(); alert("a = " + a + "; b = " + b); } X(); var x = function(){ alert(x.toString()); } x(); var x = function(){ alert(x.toString()); } x();

Hoisting JavaScript hoists all variable declarations, it moves them to the beginning of their direct scopes.

Hoisting foo(); // undefined ("foo" and "bar" exist) function foo() { alert(bar); } var bar; function foo() { alert(bar); } var bar; foo(); // undefined

Hoisting var scope = "global "; function f( ) { alert(scope); var scope = "local"; alert(scope); } f( ); var scope = "global "; function f( ) { alert(scope); var scope = "local"; alert(scope); } f( ); var scope = "global "; function f( ) { var scope; alert(scope); scope = "local"; alert(scope); } f( ); var scope = "global "; function f( ) { var scope; alert(scope); scope = "local"; alert(scope); } f( );

Only functions introduce new scopes No block scope function f() { { // block starts var foo = 4; } // block ends console.log(foo); // 4 }

function test(o) { var i = 0; // i is defined throughout function if (typeof o == "object") { var j = 0; // j is defined everywhere, not just block for(var k=0; k < 10; k++) { // k is defined everywhere, not just loop document.write(k); } document.write(k); // k is still defined: prints 10 } document.write(j); // j is defined, but may not be initialized } function test(o) { var i = 0; // i is defined throughout function if (typeof o == "object") { var j = 0; // j is defined everywhere, not just block for(var k=0; k < 10; k++) { // k is defined everywhere, not just loop document.write(k); } document.write(k); // k is still defined: prints 10 } document.write(j); // j is defined, but may not be initialized } No block scope!

var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); Hoisting

Code in global scope Untitled Page var a = 5; var b = 2; function sum(x, y) { return x + y; } function mul(x, y) { return x * y; } Untitled Page var a = 5; var b = 2; function sum(x, y) { return x + y; } function mul(x, y) { return x * y; } Index.html var gloabalVar = 5; function square(x) { return x * x; } var gloabalVar = 5; function square(x) { return x * x; } script.js

Global namespace Every variable is global unless it's in a function and is declared with var function f(){ x = “global variable”; } f(); function f(){ x = “global variable”; } f();

Global object (WAT) function f(){ x = “global variable”; // var is missed } f(); this.x === “global variable”; window.x === “global variable”; // true for browsers function f(){ x = “global variable”; // var is missed } f(); this.x === “global variable”; window.x === “global variable”; // true for browsers

window vs global (function (glob) { // glob points to global object }(typeof window !== 'undefined' ? window : global));

Global variables are evil They are less robust, behave less predictably, and are less reusable. Name clashes. Your code, built-ins, analytics code, social media buttons use the same global scope.

Globals // antipattern function sum(x, y) { // implied global result = x + y; return result; } // antipattern function foo() { var a = b = 0; //... } // preferred function foo() { var a, b; //... a = b = 0; // both local } // antipattern function sum(x, y) { // implied global result = x + y; return result; } // antipattern function foo() { var a = b = 0; //... } // preferred function foo() { var a, b; //... a = b = 0; // both local }

if (("a" in window) == false) { var a = 1; } alert(a);

Working with global window.foo if (window.foo) { … } if (“foo” in window) { … } if (typeof “foo” !== “undefined”) { … }

Namespaces if (window.myNamespace == null){ window.myNamespace = {}; } window.myNamespace.myFunction = function(/* params*/ ) { /* code here */ }; if (window.myNamespace == null){ window.myNamespace = {}; } window.myNamespace.myFunction = function(/* params*/ ) { /* code here */ };

immediately invoked function expression in next lecture

REFERENCES JavaScript: The Definitive Guide, Six Edition by David Flanagan Speaking JavaScript: An In-Depth Guide for Programmers by Dr. Axel Rauschmayer