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

Similar documents
JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript Lecture 1

welcome to BOILERCAMP HOW TO WEB DEV

JavaScript: the Big Picture

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

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

Node.js Training JavaScript. Richard richardrodger.com

CISC 1600 Lecture 2.4 Introduction to JavaScript

INF5750. Introduction to JavaScript and Node.js

JavaScript Programming

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

a Very Short Introduction to AngularJS

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

Comprehensive AngularJS Programming (5 Days)

Javascript. Many examples from Kyle Simpson: Scope and Closures

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

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

An Introduction to TypeScript. Personal Info

Session 6. JavaScript Part 1. Reading

One language to rule them all: TypeScript. Gil Fink CEO and Senior Consultant, sparxys

JavaScript or: How I Learned to Stop Worrying and Love a Classless Loosely Typed Language

JavaScript or: HOW I LEARNED TO STOP WORRYING AND LOVE A CLASSLESS*

Advanced React JS + Redux Development

Full Stack boot camp

Front End Nanodegree Syllabus

DECOUPLING PATTERNS, SERVICES AND CREATING AN ENTERPRISE LEVEL EDITORIAL EXPERIENCE

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

"Charting the Course... MOC A: Developing with the SharePoint Framework. Course Summary

Advance Mobile& Web Application development using Angular and Native Script

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

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

JavaScript: Sort of a Big Deal,

JavaScript: Introduction, Types

AngularJS Fundamentals

Such JavaScript Very Wow

Front End Nanodegree Syllabus

Treating Framework Fatigue With JavaScript

Today. Continue our very basic intro to JavaScript. Lambda calculus

JavaScript Patterns O'REILLY* S toy an Stefanov. Sebastopol. Cambridge. Tokyo. Beijing. Farnham K8ln

JavaScript Specialist v2.0 Exam 1D0-735

55249: Developing with the SharePoint Framework Duration: 05 days

Modern and Responsive Mobile-enabled Web Applications

Javascript Arrays, Object & Functions

TechWatch Report Javascript Libraries and Frameworks

Variables and Typing

Object-Oriented Programming

Web Application Development

Overview. Elements of Programming Languages. Objects. Self-Reference

CHAPTER 6 JAVASCRIPT PART 1

Think like an Elm developer

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

JavaScript. The Bad Parts. Patrick Behr

COMP519 Web Programming Lecture 14: JavaScript (Part 5) Handouts

Frontend UI Training. Whats App :

Using JavaScript on Client and Server with Project Phobos

HTML5 Evolution and Development. Matt Spencer UI & Browser Marketing Manager

JAVA: A Primer. By: Amrita Rajagopal

Ten interesting features of Google s Angular Project

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems

Client-Side Web Technologies. JavaScript Part I

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

Course 2320 Supplementary Materials. Modern JavaScript Best Practices

Frontend guide. Everything you need to know about HTML, CSS, JavaScript and DOM. Dejan V Čančarević

CS312: Programming Languages. Lecture 21: JavaScript

JavaScript for C# Programmers Kevin

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

20480C: Programming in HTML5 with JavaScript and CSS3. Course Code: 20480C; Duration: 5 days; Instructor-led. JavaScript code.

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

HTML CSS JAVASCRIPT WEB PUBLISHING IN ONE HOUR A DAY SAMS TEACH YOURSELF COVERING HTML5 CSS3 AND JQUERY 7TH EDITION

jquery with Fundamentals of JavaScript Training

Etanova Enterprise Solutions

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

When learning coding, be brave

mismatch between what is maybe possible today and what is going on in many of today's IDEs.

Standard 1 The student will author web pages using the HyperText Markup Language (HTML)

Full Stack Web Developer

Future Web App Technologies

IDM 231. Functions, Objects and Methods

Java Interfaces. 6 April 2016 OSU CSE 1

Html5 Css3 Javascript Interview Questions And Answers Pdf >>>CLICK HERE<<<

Closure Compiler: Speeding Web Appications by Compiling JavaScript

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

Cookies, sessions and authentication

IN4MATX 133: User Interface Software

CS50 Quiz Review. November 13, 2017

"Charting the Course... Comprehensive Angular. Course Summary

Modular JavaScript. JC Franco. Blake Stearman

TypeScript coding JavaScript without the pain

What is Class? Remember

Object-Oriented Design

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

JavaScript Functions, Objects and Array

Front End Programming

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

Part 1: jquery & History of DOM Scripting

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CE881: Mobile & Social Application Programming

CGS 3066: Spring 2015 JavaScript Reference

Transcription:

JavaScript: the language of browser interactions Claudia Hauff TI1506: Web and Database Technology ti1506-ewi@tudelft.nl

Densest Web lecture of this course. Coding takes time. Be friendly with Codecademy & Co. 2

At the end of this lecture, you should be able to Employ OO principles in JavaScript coding Explain the principle of callbacks Write interactive Web applications based on click, mouse and keystroke events Translate jquery-based code into jquery-less code 3

Chapter 4 of the Web course book How to include JavaScript in your Web app Essential JavaScript built-in types & control structures How to declare variables & functions The purpose of console.log() How to work with arrays How to use basic jquery features

JavaScript s reputation Until fairly recently it was considered more of a toy language Today: (most) important language of the modern Web stack Tooling has vastly improved (debuggers, testing frameworks, etc.) JavaScript runtime engines are efficient (especially V8) JavaScript tracks ECMAScript

A language in flux ES5 was published in 2009, ES6 in 2015, ES7 in 2016. ES8 (or ES2017) was finalised in June 2017. Yearly releases promised. http://kangax.github.io/compat-table/

this course: plain JavaScript Compiling to JavaScript CoffeeScript TypeScript (Microsoft) Dart (Google) JavaScript All major languages compile to JS https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js 7

Scripting overview

Requesting & processing a Web page in 4 steps 1 Browser sends a GET request to a Web server 2 4 Execution of server-side scripts (PHP, ASP, node.js, etc.) 3 Web server sends (generated) resources to the browser Execution of scripts (*.js) & rendering of page JavaScript makes Web apps interactive and responsive to user actions.

Server-side scripting Source code is private, result of script execution is returned (in HTML), not the script itself HTML can be rendered by any browser Server-side scripts can access additional resources (including databases) Server-side scripts can use non-standard language features (you know your server s software)

Client-side scripting Source code is visible to everyone

Client-side scripting Source code is visible to everyone Script execution by the browser reduces load on the Web server All raw data necessary (e.g. for visualizations) needs to be downloaded and processed by the client JavaScript is event-driven: code blocks executed in response to user actions (click, hover, move, etc.)

Objects in JavaScript Basic constructor Prototype-based constructor Module pattern

OO for JavaScript JavaScript has functions as first-class citizens OO groups together related data and behaviour Built-in objects: String, Number, Array, etc. Objects can be created in different ways (do not get confused, stick to one way) A value has first-class status if it can be passed as a parameter, returned from a subroutine, or assigned into a variable. (Michael L. Scott)

Design patterns Design patterns are reusable solutions to commonly occurring problems in software design. Addy Osmani Many design patterns exist, we focus on three (the most important ones for our use case) Design patterns develop over time Design patterns often hold across programming languages

Objects in JavaScript new Object() produces an empty object, ready to receive name/value pairs Name: any string Value: anything (String, array, Number, etc.) apart from undefined Members are accessed through [name] (bracket notation).name (dot notation)

Another way: object literals

Adding a method note1.tostring() note2.tostring()

Object literals can be complex inner object Param

Are object literals enough? What happens if we need 1,000 objects of this kind? What happens if a method needs to be added to all objects?

Design Pattern (1): Basic constructor

Recall: constructors in Java

Basic constructor in JavaScript In JavaScript, functions are first-class citizens.

this can refer to anything! Basic constructor An object constructor is just a normal function What does JavaScript do with new? new anonymous empty object is created and used as this returns new object at the end of the function common error: forgetting new

Basic constructor New variables and objects can be added on the fly. Objects come with default methods (prototype chaining)

Summary: basic constructor Advantage: very easy to use Issues: Not obvious how to use inheritance (e.g. NoteWithDueDate) Objects do not share functions function tostring() is not shared between note1 and note2 All members are public Any piece of code can access/change/delete(!) members type and note 26

Design Pattern (2): Prototype-based constructor

Prototype chaining explained Objects have a secret pointer to another object - the object's prototype Properties of the constructor's prototype are also accessible in the new object If a member is not defined in the object, the prototype chain is followed var name = Daisy ; typeof(name); // string name.charat(1) proto String.prototype charat() indexof() 28

Prototype-based constructor 29

Getting to grips with JavaScript WebConsole is your friend!

Prototype-based constructor Prototype changes are also reflected in existing objects! 31

Prototype-based constructor Inheritance through prototyping. call() calls a function with a given this value and arguments (one by one) 1. create a new constructor 2. redirect the prototype = prototype chain 32

Summary: prototype-based constructor Advantages: Inheritance is easy to achieve Objects share functions Issue: All members are public, i.e. any piece of code can access/change/delete members type and note 33

Design Pattern (3): Module

JavaScript scoping All JavaScript code enters the same namespace JavaScript has limited scoping var in function: local, limited scope var outside of a function: global scope no var: global scope (holds for function names too) let (ES6): block scope const (ES6): block scope, no reassignment or redeclaration 35

JavaScript scoping What if another JavaScript library used in the project defines note1?

Module Goals: Do not declare any global variables or functions unless required Emulate private/public membership Expose only the necessary members to the public (as API) Results: Potential conflicts with other JavaScript libraries are reduced Public API minimizes unintentional side-effects when wrongly used 37

Module 38

Module 39

Module public private 40

})();

Module the pattern can be arbitrarily complex;

Module The encapsulating function can also contain arguments

Summary: module Advantages: Encapsulation is achieved Object members are either public or private Issues: Changing the type of membership (public/ private) costs time Methods added on the fly later on cannot access 'private' members 44

Events & the DOM https://developer.mozilla.org/en-us/docs/tools

A look at book chapter 4 Uses jquery extensively (a big time saver) Important to understand what jquery "covers up Decide for yourself whether you want to use jquery in the assignments (other JavaScript libraries are not allowed) 46

A look at book chapter 4 With jquery: no matter if class or id or..., the access pattern is the same, i.e. $() Callback principle: we define what should happen when an event fires 47

$()

});

callback hell }); }); }); }); }); });

Step-by-step: making a responsive UI control 1. Pick a control (e.g. a button) 2. Pick an event (e.g. a click on a button) 3. Write a JavaScript function: what should happen when the event occurs? (e.g. a popup appears) 4. Attach the function to the event ON the control 51

Client-side JS examples [HTML SLIDES]