Javascript. Daniel Zappala. CS 360 Internet Programming Brigham Young University

Similar documents
JavaScript: Introduction, Types

CSC Web Programming. Introduction to JavaScript

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.

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

IAT 355 : Lab 01. Web Basics

JavaScript CS 4640 Programming Languages for Web Applications

CISC 1600 Lecture 2.4 Introduction to JavaScript

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

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. JavaScript. Standard

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. Standard

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

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

<script type="text/javascript"> script commands </script>

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

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

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

COMS 469: Interactive Media II

Comprehensive AngularJS Programming (5 Days)

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

JavaScript Specialist v2.0 Exam 1D0-735

Programming Basics. INFO/CSE 100, Spring 2006 Fluency in Information Technology.

2D Game design Intro to Html 5

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

Programming Basics. INFO/CSE 100, Spring 2005 Fluency in Information Technology.

JavaScript Programming

CGS 3066: Spring 2015 JavaScript Reference

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

JavaScript: Sort of a Big Deal,

Mobile Site Development

PHP. Interactive Web Systems

Session 3: JavaScript - Structured Programming

Working with JavaScript

a Why JavaScript? jonkv interactivity on the web CGI JavaScript Java Applets Netscape LiveScript JavaScript 1: Example

Client-Side Web Technologies. JavaScript Part I

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

Tutorial 10: Programming with JavaScript

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Data Visualization (CIS 468)

JavaScript Lecture 1

EXERCISE: Introduction to client side JavaScript

Netscape Introduction to the JavaScript Language

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

CERTIFICATE IN WEB PROGRAMMING

INF5750. Introduction to JavaScript and Node.js

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

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

JQUERYUI - WIDGET FACTORY

Chapter4: HTML Table and Script page, HTML5 new forms. Asst. Prof. Dr. Supakit Nootyaskool Information Technology, KMITL

Time : 3 hours. Full Marks : 75. Own words as far as practicable. The questions are of equal value. Answer any five questions.

Lecture 8: JavaScript

Introduction to Programming Using Java (98-388)

Unit Notes. ICAWEB411A Produce basic client-side script for dynamic web pages Topic 1 Introduction to JavaScript

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

Lecture 8 (7.5?): Javascript

JavaScript: Features, Trends, and Static Analysis

Chapter 6: Inheritance

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

Implementing non-static features

Node.js Training JavaScript. Richard richardrodger.com

Programing for Digital Media EE1707. Lecture 3 JavaScript By: A. Mousavi and P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Final-Term Papers Solved MCQS with Reference

ITS331 Information Technology I Laboratory

JavaScript Functions, Objects and Array

FUNDAMENTAL JAVASCRIPT

HTML5 and CSS3 More JavaScript Page 1

Scripting. Web Architecture and Information Management [./] Spring 2009 INFO (CCN 42509) Contents

JavaScript. What s wrong with JavaScript?

JavaScript Programming

c.def (pronounced SEE-def) Language Reference Manual

Lesson 11: JavaScript Libraries

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Client vs Server Scripting

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

c122mar413.notebook March 06, 2013

Exercise 1: Basic HTML and JavaScript

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

IOOR. Lecture 4 Inheritance vs. delegation, Actor-based languages, Methods vs messages, Course council! Prototype-based. Prototype-based PLs

Week 13 Thursday (with Page 5 corrections)

Boot Camp JavaScript Sioux, March 31, 2011

Modular JavaScript. JC Franco. Blake Stearman

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

The JavaScript Language

Variables One More (but not the last) Time with feeling

Functional Programming. Pure Functional Programming

Web Development & Design Foundations with HTML5

SEEM4570 System Design and Implementation Lecture 03 JavaScript

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

PHP by Pearson Education, Inc. All Rights Reserved.

5. JavaScript Basics

JavaScript: The Basics

HTML HTML. Chris Seddon CRS Enterprises Ltd 1

A340 Laboratory Session #5

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

CS312: Programming Languages. Lecture 21: JavaScript

Get Unique study materials from

Transcription:

Javascript Daniel Zappala CS 360 Internet Programming Brigham Young University

Introduction

3/35 Introduction Basics Functions Data Structures Higher-Order Functions Prototypes What You Should Read Eloquent JavaScript introductory interactive coding Why Prototypical Inheritance Matters advanced prototypical inheritance

Why Javascript? 4/35 client-side computation more responsive web user interface asynchronous communication with server dynamically change HTML being displayed by browser built into most browsers

Javascript vs Java 5/35 JavaScript has no relationship to Java Javascript is becoming what Java was meant to be lightweight, downloadable program that runs in browser and is compatible across many platforms does much of what Java applets do, with a fraction of the resources

Features 6/35 interpreted dynamic typing (delays binding of types until they are used) first-class functions (can take functions as arguments and return functions) prototypes (objects based on prototypes instead of inheritance)

Hello World 7/35 1 alert("hello world!");

Functions 8/35 1 var factorial = function(n) { 2 if (n === 0) { 3 return 1; 4 } 5 return n factorial(n 1); 6 } 7 factorial(5);

DOM Parsing and Manipulation 9/35 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www. w3.org/tr/html4/strict.dtd"> 2 <html> 3 <meta charset="utf-8"> 4 <head> 5 <title>simple page</title> 6 </head> 7 <body> 8 <h1 id="header">this is JavaScript</h1> 9 <script type="text/javascript"> 10 document.write( Hello World! ); 11 // get element with CSS ID "header" 12 var h1 = document.getelementbyid("header"); 13 console.log(h1.innerhtml); 14 // get first h1 on the page 15 h1 = document.getelementsbytagname("h1")[0]; 16 console.log(h1.innerhtml); 17 // change element 18 h1.innerhtml = "This is dope!"; 19 </script> 20 </body> 21 </html>

Basics

Variables 11/35 1 // local variable 2 var x = 12; 3 // global variable 4 y = 12;

Operators 12/35 comparison 1 > 2 < 3 >= 4 <= 5!= 6 == 7! 8 9 && 10 === // no automatic type conversion 11!== // no automatic type conversion

Operators 13/35 expressions and assignment 1 + 2 3 4 / 5 % 6 = 7 += 8 = 9 ++ 10

Control 14/35 1 if ( boolean statement ) { 2... 3 } else { 4... 5 } 1 switch (variable) { 2 case 1: 3... 4 break; 5 case 2: 6... 7 break; 8 case default: 9... 10 break; 11 }

Control 15/35 1 while (boolean condition) 2 { 3... 4 } 1 do 2 { 3... 4 } 5 while (boolean condition); 1 for (var i = 0; i < 10; i++) { 2... 3 }

Functions

Functions 17/35 1 var add = function(a,b) { 2 return a + b; 3 } 4 5 add(5,3);

Scope 18/35 1 var landscape = function() { 2 var result = ""; 3 var flat = function(size) { 4 for (var count = 0; count < size; count++) 5 result += "_"; 6 }; 7 var mountain = function(size) { 8 result += "/"; 9 for (var count = 0; count < size; count++) 10 result += " "; 11 result += "\\"; 12 }; 13 14 flat(3); 15 mountain(4); 16 flat(6); 17 mountain(1); 18 flat(1); 19 return result; 20 }; 21 22 console.log(landscape()); 23 // / \ / \_

Optional Parameters 19/35 1 function power(base, exponent) { 2 if (exponent == undefined) 3 exponent = 2; 4 var result = 1; 5 for (var count = 0; count < exponent; count++) 6 result = base; 7 return result; 8 } 9 10 console.log(power(4)); 11 // 16 12 console.log(power(4, 3)); 13 // 64

Closure 20/35 1 var increment = function() { 2 var count = 0; 3 return function () { 4 return ++count; 5 }; 6 } 7 console.log(increment()); 8 // 1 9 console.log(increment()); 10 // 2

Data Structures

Lists 22/35 1 var list = [2, 3, 5, 7, 11]; 2 console.log(list[1]); 3 // 3 4 console.log(list.length); 5 // 5 6 list.push(13,17); 7 console.log(list); 8 // [ 2, 3, 5, 7, 11, 13, 17 ]

Objects 23/35 1 var user = { 2 loggedin: false, 3 items: ["homework", "read The Martian", "play frisbee golf"], 4 whatshouldido: function() { 5 return "You should be doing " + this.items[0] 6 } 7 }; 8 console.log(user.loggedin); 9 // false 10 console.log(user.name); 11 // undefined 12 user.name = "Emma"; 13 console.log(user.name); 14 // Emma 15 console.log(user.whatshouldido()); 16 // You should be doing homework

Higher-Order Functions

foreach 25/35 executes a function for each element in a list 1 var pets = ["dog","cat","lizard"] 2 pets.foreach(function(pet) { 3 console.log(pet.touppercase()); 4 }); 5 // DOG 6 // CAT 7 // LIZARD

filter 26/35 returns a new list by applying a function that checks for membership 1 var housepets = pets.filter(function(pet) { 2 return (pet === "dog" pet == "cat"); 3 }); 4 console.log(housepets); 5 // [ dog, cat ]

map 27/35 creates a new list by applying a function to transform all elements 1 var uppers = pets.map(function(pet) { 2 return pet.touppercase(); 3 }); 4 console.log(uppers); 5 // [ DOG, CAT, LIZARD ]

reduce 28/35 returns a value by applying a function to all elements 1 var total = pets.map(function(pet) { 2 return pet.length; 3 }).reduce(function(a,b) { 4 return a + b; 5 }); 6 console.log(total); 7 // 12

Prototypes

Prototypes 30/35 Object.create() clones an object, specifying its prototype 1 var rectangle = { 2 area: function () { 3 return this.width this.height; 4 } 5 }; 6 var rect = Object.create(rectangle); 7 rect.width = 5; 8 rect.height = 10; 9 console.log(rect.area()); see Why Prototypical Inheritance Matters

Simplifying Object Creation 31/35 wrap create into a function 1 var rectangle = { 2 create: function (width, height) { 3 var self = Object.create(this); 4 self.height = height; 5 self.width = width; 6 return self; 7 }, 8 area: function () { 9 return this.width this.height; 10 } 11 }; 12 var rect = rectangle.create(5, 10); 13 console.log(rect.area());

Overriding Functions 32/35 square calls its own create(), which calls create() of rectangle square calls area() on rectangle 1 var square = Object.create(rectangle); 2 3 square.create = function (side) { 4 return rectangle.create.call(this, side, side); 5 }; 6 var sq = square.create(5); 7 console.log(sq.area());

Extending Objects 33/35 extend() creates a new object that inherits properties from this (delegation) and copies properties from extension (concatenation) 1 Object.prototype.extend = function (extension) { 2 var hasownproperty = Object.hasOwnProperty; 3 var object = Object.create(this); 4 5 for (var property in extension) 6 if (hasownproperty.call(extension, property) 7 typeof object[property] === "undefined") 8 object[property] = extension[property]; 9 10 return object; 11 };

Extending The Square 34/35 extends rectangle with create 1 var square = rectangle.extend({ 2 create: function (side) { 3 return rectangle.create.call(this, side, side); 4 } 5 }); 6 7 var sq = square.create(5); 8 9 console.log(sq.area());

Extending The Rectangle 35/35 extends Object with height, width 1 var rectangle = { 2 create: function (width, height) { 3 return this.extend({ 4 height: height, 5 width: width 6 }); 7 }, 8 area: function () { 9 return this.width this.height; 10 } 11 }; 12 13 var rect = rectangle.create(5, 10); 14 console.log(rect.area());