Lecture III.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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,
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.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
Taking JavaScript Seriously IS NOT THE WORST IDEA.
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.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Martin Kruliš by Martin Kruliš (v1.0)1.
Functions and Closures. JavaScript Closures Are Everywhere In JS we often want to say “when this thing happens, do something” event driven programming.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
Function the Ultimate Act III. Function Method Class Constructor Module.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
JavaScript scope and closures
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript for C# developers Dhananjay Microsoft MVP
Lecture III. Example var a = 2; var b = 2; console.log(a + b);
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.
C Programming Lecture 8 Call by Reference Scope. Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must.
REEM ALMOTIRI Information Technology Department Majmaah University.
Build in Objects In JavaScript, almost "everything" is an object.
CGS 3066: Web Programming and Design Spring 2017
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Exam 2 Review.
>> JavaScript: Location, Functions and Objects
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
SPC Developer 6/10/2018 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Python: Control Structures
Barb Ericson Georgia Institute of Technology May 2006
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
JavaScript: Functions
Scope, Objects, Strings, Numbers
JavaScript Functions.
Functions Declarations, Function Expressions and IIFEs
Chapter 19 JavaScript.
JavaScript and Ajax (Expression and Operators)
Names, Scopes, and Bindings: Scopes
Scope of Variables.
HCI Lab 2.
The Web Wizard’s Guide To JavaScript
CSC215 Lecture Flow Control.
Functions, Regular expressions and Events
CSC215 Lecture Control Flow.
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
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Conditional Statements with JavaScript
Chapter 20: Programming Functions
JavaScript CS 4640 Programming Languages for Web Applications
The <script> Tag
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
CS3220 Web and Internet Programming JavaScript Basics
Programming Basics Review
Topics to cover Instance variables vs. local variables.
Programming II Vocabulary Review Loops & functions
JavaScript CS 4640 Programming Languages for Web Applications
Scope Rules.
Programming Methodology
Functions, Part 1 of 2 Topics Using Predefined Functions
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 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); } outer scope inner 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();

Shadowing 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();

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();

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"; } f( ); var scope = "global "; function f( ) { var scope; alert(scope); scope = "local"; } f( );

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

No block scope! 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 console.log (k); } console.log (k); // k is still defined: prints 10 console.log (j); // j is defined, but may not be initialized

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

Code in global scope Index.html script.js var gloabalVar = 5; <!DOCTYPE html> <html> <head> <title>Untitled Page</title> <script type="text/javascript" src="script.js"></script> </head> <body> <script type="text/javascript"> var a = 5; var b = 2; function sum(x, y) { return x + y; } function mul(x, y) { return x * y; </script> </body> </html> var gloabalVar = 5; function square(x) { return x * x; } script.js

Global namespace function f(){ x = “global variable”; } f(); Every variable is global unless it's in a function and is declared with var 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

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; } function foo() { var a = b = 0; // ... // preferred var a, b; a = b = 0; // both local

<script> if (("a" in window) == false) { var a = 1; } alert(a); </script>

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 */ };

immediately invoked function expression and ES6 Modules in next lectures

REFERENCES JavaScript: The Definitive Guide, Six Edition by David Flanagan Speaking JavaScript: An In-Depth Guide for Programmers by Dr. Axel Rauschmayer http://dmitrysoshnikov.com http://learn.javascript.ru