JavaScript. The Bad Parts. Patrick Behr

Similar documents
JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

CGS 3066: Spring 2015 JavaScript Reference

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

CS312: Programming Languages. Lecture 21: JavaScript

INF5750. Introduction to JavaScript and Node.js

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

JS Event Loop, Promises, Async Await etc. Slava Kim

Javascript. Many examples from Kyle Simpson: Scope and Closures

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

Client-Side Web Technologies. JavaScript Part I

JavaScript: The Definitive Guide

Working with JavaScript

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Node.js Training JavaScript. Richard richardrodger.com

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Web Application Development

Princess Nourah bint Abdulrahman University. Computer Sciences Department

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JavaScript: More Syntax

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

JQuery and Javascript

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

JavaScript Introduction

Chris Buckett. FOREWORD BY Seth Ladd MANNING 6$03/(&+$37(5

3 The Building Blocks: Data Types, Literals, and Variables

JavaScript Lecture 1

JavaScript for PHP Developers

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

JavaScript Functions, Objects and Array

Outline. Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages

JavaScript Programming

JavaScript: Sort of a Big Deal,

Varargs Training & Software Development Centre Private Limited, Module: HTML5, CSS3 & JavaScript

JavaScript: the Big Picture

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

! The final is at 10:30 am, Sat 6/4, in this room. ! Open book, open notes. ! No electronic devices. ! No food. ! Assignment 7 due 10pm tomorrow

COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Web Development. HCID 520 User Interface Software & Technology

iframe programming with jquery jquery Summit 2011

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

PHP by Pearson Education, Inc. All Rights Reserved.

CSC Web Programming. Introduction to JavaScript

Key Differences Between Python and Java

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

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

JavaScript: The Basics

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

B l o c k B i n d i n g s

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

B ON U S C H A P T E R MANNING. Simon Holmes

JavaScript: More Syntax and Using Events

JavaScript: Introduction, Types

Lesson: Web Programming(6) Omid Jafarinezhad Sharif University of Technology

JavaScript Programming

JavaScript Lecture 2

E ECMAScript, 21 elements collection, HTML, 30 31, 31. Index 161

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

What is PHP? [1] Figure 1 [1]

Kyle Rainville Littleton Coin Company

Asynchronous Programming in Javascript, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 19 10/25/2016

Seema Sirpal Delhi University Computer Centre

JavaScript I Language Basics

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

Jquery Manually Set Checkbox Checked Or Not

CHIL CSS HTML Integrated Language

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

JavaScript for C# Programmers Kevin

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

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Lesson: Web Programming(4) Omid Jafarinezhad Sharif University of Technology

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Java SE7 Fundamentals

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

Design Programming DECO2011

Index. Ray Nicholus 2016 R. Nicholus, Beyond jquery, DOI /

JAVASCRIPT MOCK TEST JAVASCRIPT MOCK TEST I

Chapter 3 Data Types and Variables

DINO. Language Reference Manual. Author: Manu Jain

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

1B1b Classes in Java Part I

Today: Distributed Middleware. Middleware

Concurrency User Guide

JavaScript Basics. Mendel Rosenblum. CS142 Lecture Notes - JavaScript Basics

Intro To Javascript. Intro to Web Development

Govt. of Karnataka, Department of Technical Education Diploma in Computer Science & Engineering. Fifth Semester. Subject: Web Programming

Chapter 6: JavaScript for Client-Side Computation and Form Data Validation

THE NEW ERA OF WEB DEVELOPMENT. qooxdoo. Andreas Ecker, Derrell Lipman

CHAPTER 6 JAVASCRIPT PART 1

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Abstract. 1. Introduction. 2. AJAX overview

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

IBM. BPM Blueprint; IBM WebSphere Lombardi Edition V7.1, Application Development

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic


Transcription:

JavaScript The Bad Parts Patrick Behr

History Created in 1995 by Netscape Originally called Mocha, then LiveScript, then JavaScript It s not related to Java ECMAScript is the official name Many implementations (V8, JavaScript, Chakra, JScript.NET, ActionScript) 2005 Jesse James Garrett introduces Ajax 2009 ECMAScript 5 and Harmony 2015 ECMAScript 2015 (the 6 th edition) ECMAScript 2016, ECMAScript 2017

What is JavaScript HTML content and structure CSS style and layout JavaScript Programming language the runs in the browser Make your web pages interactive Dynamically update content Add or remove elements Animate images Modify styles Validate form data

What does JavaScript look like Variables Must start with letter, number, underscore, or dollar sign Case sensitive: myvariable, myvariable, MYVARIABLE You must declare a variable before you can reference it You don t define a data type

What does JavaScript look like

What does JavaScript look like Primitive Data Types Boolean Number String Null Undefined Objects Date Array

What does JavaScript look like Functions Can accept input parameters (up to 255) Can return a value If no return statement, it will return undefined by default Can have a name, but doesn t have to Can be assigned to a variable and passed as a parameter Must be defined before you can call it

What does JavaScript look like Function declaration

What does JavaScript look like Objects Key / Value pairs Surrounded by curly brackets Can contain variables, functions, and objects Can also be empty

What does JavaScript look like

How JavaScript does stuff Variable type is determined by it s value Variables can change type Use typeof to determine variable type

How JavaScript does stuff

How JavaScript does stuff Assignment =

How JavaScript does stuff Equality == and ===

How JavaScript does stuff Non-Equality!= and!==

How JavaScript does stuff Remember not to check equality using =

How JavaScript does stuff Remember not to check equality using = ASSIGNMENT

How JavaScript does stuff Remember not to check equality using =

You want the truthy? You can t handle the truthy! Falsey values false null undefined 0 (zero) (blank) Truthy If it s not falsey, it must be truthy

How JavaScript does stuff JavaScript is executed line by line Variables and functions must be defined before used

How JavaScript does stuff

How JavaScript does stuff You can use variables and functions prior to definition

How JavaScript does stuff

How JavaScript does stuff Referred to as Hoisting Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope w3schools

How JavaScript does stuff

How JavaScript does stuff

How JavaScript does stuff

How JavaScript does stuff

How JavaScript does stuff Declarations aren t actually moved anywhere Two phases for each new lexical scope (global, function) Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations Execution 1. Execute line by line

How JavaScript does stuff

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations

How JavaScript does stuff DECLARATION Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations

How JavaScript does stuff DECLARATION Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations REGISTERED INTERNALLY myvar = undefined

How JavaScript does stuff ASSIGNMENT Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations myvar = undefined

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations Execution 1. Execute line by line myvar = undefined

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations Execution 1. Execute line by line myvar = undefined

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations Execution 1. Execute line by line myvar = My variable

How JavaScript does stuff

How JavaScript does stuff

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations REGISTERED INTERNALLY DECLARATION myvar = function

How JavaScript does stuff Creation 1. Function parameters 2. Scan for function declarations 3. Scan for variable declarations myvar already registered DECLARATION myvar = function

How JavaScript does stuff myvar = function

How JavaScript does stuff myvar = function

How JavaScript does stuff myvar = This is a string

How JavaScript does stuff myvar = This is a string

Uncle Aesop says Define variables and functions at the top

Review Variables Declaration Assignment Equality

Review Variables Declaration Assignment Equality Data Types Primitive Data Types Boolean Number String Null Undefined Objects Date Array

Review Variables Declaration Assignment Equality Data Types Functions

Review Variables Declaration Assignment Equality Data Types Functions Objects

Review Variables Declaration Assignment Equality Data Types Functions Objects Hoisting

Functions and Callbacks

A closer look at functions Can have a name, but can also be anonymous (unnamed) First class objects Stored in variables, passed into or returned from functions Often used as a Callback A callback is a function sent as an argument into another function The callback function is called from the other function

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

Uncle Aesop says It s a good idea to make sure the callback is actually a function before you call it!

Uncle Aesop says It s a good idea to make sure the callback is actually a function before you call it!

Synchronous and asynchronous functions Synchronous In-line function call The main function waits for the called function to complete Like a phone call Asynchronous Similar to a submitted job The main function is free to continue processing Like a text message

Synchronous and asynchronous functions Synchronous

Synchronous and asynchronous functions Synchronous QINTER QBATCH Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER QBATCH SYNC STARTED Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER QBATCH SYNC STARTED OtherPgm Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER OtherPgm Program1 QBATCH SYNC STARTED OTHER PGM QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER QBATCH SYNC STARTED OTHER PGM Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER QBATCH SYNC STARTED OTHER PGM SYNC ENDING Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Synchronous QINTER QBATCH SYNC STARTED OTHER PGM SYNC ENDING QINTER JOB QUEUE

Synchronous and asynchronous functions Asynchronous

Synchronous and asynchronous functions Asynchronous QINTER QBATCH Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED Program1 OtherPgm QINTER JOB QUEUE

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED ASYNC ENDING Program1 OtherPgm QINTER JOB QUEUE

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED ASYNC ENDING QINTER JOB QUEUE OtherPgm

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED ASYNC ENDING OTHER PGM QINTER JOB QUEUE OtherPgm

Synchronous and asynchronous functions Asynchronous QINTER QBATCH ASYNC STARTED ASYNC ENDING OTHER PGM QINTER JOB QUEUE

Synchronous and asynchronous functions Callback Program to callback

Synchronous and asynchronous functions Callback QINTER QBATCH Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED Program1 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED Program1 OtherPgm QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE Program1 OtherPgm QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM Program1 OtherPgm QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM Program1 OtherPgm Program2 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM Program1 Program2 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER Program1 QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM ASYNC ENDED Program2 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM ASYNC ENDED Program2 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM ASYNC ENDED Program2 QINTER JOB QUEUE

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM ASYNC ENDED Program2 QINTER JOB QUEUE PROGRAM2

Synchronous and asynchronous functions Callback QINTER QBATCH ASYNC STARTED ASYNC CONTINUE OTHER PGM ASYNC ENDED PROGRAM2 QINTER JOB QUEUE

Concurrency model and Event Loop Each function call form a stack of frames Each frame contains the functions arguments and variables When the function is done, the frame is removed from the stack WebAPIs Functionality (APIs) provided by the browser settimeout Animation stuff Callback Queue

Concurrency model and Event Loop STACK WebAPIs CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting console.log main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting settimeout main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting settimeout main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting ending console.log main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting ending main() CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting ending CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs timer... starting ending callback CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting ending callback CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting ending set timeout console.log CALLBACK QUEUE

Concurrency model and Event Loop STACK WebAPIs starting ending set timeout CALLBACK QUEUE

Callback nesting

Callback nesting, nesting, nesting, HELL