FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Similar documents
JavaScript for C# Programmers Kevin

CS312: Programming Languages. Lecture 21: JavaScript

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

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript: Sort of a Big Deal,

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Client-Side Web Technologies. JavaScript Part I

Boot Camp JavaScript Sioux, March 31, 2011

INF5750. Introduction to JavaScript and Node.js

JavaScript. History. Adding JavaScript to a page. CS144: 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.

JavaScript. The Bad Parts. Patrick Behr

CSC Web Programming. Introduction to JavaScript

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

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

Node.js Training JavaScript. Richard richardrodger.com

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

Lecture 8 (7.5?): Javascript

JavaScript for PHP Developers

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

Web Application Development

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

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

Advanced Topics in Programming Languages Spring Semester, Lecture 2: March 13, JavaScript

INF3110 Programming Languages Runtime Organization part II

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

Organizing Code in Web Apps. SWE 432, Fall 2017 Design and Implementation of Software for the Web

CITS1231 Web Technologies. JavaScript

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

JAVA MOCK TEST JAVA MOCK TEST II

Notes on JavaScript (aka ECMAScript) and the DOM

Spring JavaScript. John Mitchell Adapted by Mooly Sagiv. Reading: links on last slide Homework 1: 18/3 17/4

Javascript. Many examples from Kyle Simpson: Scope and Closures

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

JavaScript: Introduction, Types

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

low and not larger than high. 18 points

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

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

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

Introduction to Programming Using Java (98-388)

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

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

CS558 Programming Languages

Comprehensive AngularJS Programming (5 Days)

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Pace University. Fundamental Concepts of CS121 1

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

JavaScript Programming

CGS 3066: Spring 2015 JavaScript Reference

COMP 2406: Fundamentals of Web Applications. Fall 2013 Mid-Term Exam Solutions

Class, Variable, Constructor, Object, Method Questions

Contents. Chapter 1 Overview of the JavaScript C Engine...1. Chapter 2 JavaScript API Reference...23

Variable Declarations

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

JavaScript Introduction

Java Primer 1: Types, Classes and Operators

More on JavaScript Functions

Java: introduction to object-oriented features

Working with JavaScript

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Ruby: Introduction, Basics

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

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

Programming II (CS300)

CS 251 Intermediate Programming Methods and Classes

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

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

Object Oriented Paradigm Languages

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS 130(0) JavaScript Lab

Lecture 16: Static Semantics Overview 1

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

The JavaScript Language

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Declarations and Access Control SCJP tips

Such JavaScript Very Wow

Object Oriented JavaScript (Part the Second)

CSE 307: Principles of Programming Languages

1 Shyam sir JAVA Notes

Chapter 3 Data Types and Variables

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Implementing Subprograms

Princess Nourah bint Abdulrahman University. Computer Sciences Department

INF3110 Programming Languages Runtime Organization part II

Inheritance. Transitivity

Ruby: Introduction, Basics

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

JavaScript: the Big Picture

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

CS 251 Intermediate Programming Methods and More

Organizing Code in Web Apps. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Transcription:

CS 498RK FALL 2017 JAVASCRIPT Fashionable and Functional!

JAVASCRIPT popular scripting language on the Web, supported by browsers separate scripting from structure (HTML) and presentation (CSS) client- and server-side programming object-oriented, imperative, functional

Timeline 1993: 1st HTML spec Tim Berners-Lee 2000: JS 1.5 ECMAScript Ed. 3 HTML2 1989 2015 1995: JavaScript Brendan Eich Developed at Netscape for Navigator 2 Standardized for browser compatibility

JAVA : JAVASCRIPT :: :

VARIABLES Dynamically typed: types associated with values, not with variables Use var to define local variables variables defined implicitly through assignment have global scope

BASIC DATA TYPES Booleans: true, false Number: no integers, 64-bit floating point String: no char, variable-length Special Types: null, undefined get familiar with String methods

ARRAYS var classes = new Array(); classes[3] = cs199rk ; var numbers = [5,3,2,6]; numbers.length; other methods: push, pop, sort,

OBJECTS collection of properties: name-value pairs llama = {color: brown, age:7, hasfur: true; add new properties on the fly llama.family = camelid ;

At first blush var sum = 0; var numbers = [5,3,2,6]; for (var i=0;i<numbers.length;i++) { sum += numbers[i]; everything seems typical

Functions are first-class objects

FUNCTIONS ARE OBJECTS that are callable! reference by variables, properties of objects pass as arguments to functions return as values from functions can have properties and other functions

DECLARATION function eat() { var sleep = function() { console.log(eat.name); console.log(sleep.name); what will this print?

DECLARATION name function eat() { var sleep = function() { anonymous function

ANONYMOUS FUNCTIONS create a function for later use store it in a variable or method of an object use it as a callback see more examples next class

SORT COMPARATOR var inventory = [{product: tshirt,price:15.00, {product: jacket,price:35.00, {product: shorts,price:10.00] inventory.sort(function(p1,p2){ return p1.price-p2.price;);

VARIABLE NUMBER OF ARGUMENTS functions handle variable number of arguments excess arguments are accessed with arguments parameter unspecified parameters are undefined

this the other implicit parameter a.k.a. function context object that is implicitly associated with a function s invocation defined by how the function is invoked (not like Java)

FUNCTION INVOCATION function eat() {return this; eat(); var sleep = function() {return this; sleep(); this refers to the global object

METHOD INVOCATION function eat() {return this; var llama = { graze: eat ; var alpaca = { graze: eat ; this refers to the object console.log(llama.graze()===llama); true console.log(alpaca.graze()===alpaca); true

apply()and call() two methods that exist for every function explicitly define function context apply(functioncontext,arrayofargs) call(functioncontext,arg1,arg2, )

implemented in Javascript 1.6 function foreach(list, callback){ for (var n = 0; n < list.length; n++){ callback.call(list[n],n); var numbers = [5,3,2,6]; foreach(numbers, function(index){ numbers[index]= this*2;); console.log(numbers);

don t need multiple copies of a function to operate on different kinds of objects! function foreach(list, callback){ for (var n = 0; n < list.length; n++){ callback.call(list[n],n); var camelids = ["llama", "alpaca", "vicuna"]; foreach(camelids, function(index){ camelids[index]= this+this;); console.log(camelids);

Classes are defined through functions

OBJECT-ORIENTED PROGRAMMING new operator applied to a constructor function creates a new object no traditional class definition newly created object is passed to the constructor as this parameter, becoming the constructor s function context constructor returns the new object

CONSTRUCTOR INVOCATION function Llama() { constructors are given the class name this.spitted = false; this.spit = function() { this.spitted = true; var llama1 = new Llama(); llama1.spit(); console.log(llama1.spitted); true var llama2 = new Llama(); console.log(llama2.spitted); false

prototype prototype is a property of the constructor another way to add methods to objects function Llama() { this.spitted = false; Llama.prototype.spit = function() { this.spitted = true; ;

function Llama() { this.spitted = false; this.spit = function() { this.spitted = true; Llama.prototype.spit = function() { this.spitted = false; ; var llama1 = new Llama(); llama1.spit(); console.log(llama1.spitted); true

var llama1 Object property constructor binding operations within Constructor property prototype the constructor always take precedence over those in its prototype Prototype Object

INHERITANCE create prototype as instance of parent class Llama.prototype = new Camelid();

PROTOTYPE CHAINING var llama1 if a property isn t in Llama, look in Camelid, and so on instanceof Llama property constructor instanceof Camelid property constructor Llama() property prototype Camelid() property prototype

Scoping

SCOPE function outerfunction() { var x = 1; function innerfunction() { if(x==1) {var y=2; console.log(y); what will it print? outerfunction();

scopes are declared through functions and not blocks {

HOISTING Variables and functions are in scope within the entire function they are declared in

SCOPE function outerfunction() { var x = 1; function innerfunction() { if(x==1) {var y=2; x, y console.log(y); outerfunction();

SCOPE function outerfunction() { var x = 1; function innerfunction() { if(x==1) {var y=2; innerfunction console.log(y); outerfunction(); outerfunction

HOISTING function outerfunction() { var x = 1; console.log(y); what will it print? if(x==1) {var y=2; outerfunction(); initializations are not hoisted!

closure scope created when a function is declared that allows the function to access and manipulate variables that are external to that function

CLOSURES access all the variables (including other functions) that are in-scope when the function itself is declared inner function has access to state of its outer function even after the outer function has returned!

var outervalue = 'llama'; Closure Example var later; function outerfunction() { var innervalue = 'alpaca'; function innerfunction() { console.log(outervalue); console.log(innervalue); what will this print? later = innerfunction; outerfunction(); later();

var outervalue = 'llama'; Closure Example var later; function outerfunction() { var innervalue = 'alpaca'; function innerfunction() { console.log(outervalue); console.log(innervalue); later = innerfunction; outerfunction(); prints: llama alpaca innerfunction has access to innervalue through its closure later();

var outervalue = 'llama'; Closure of innerfunction var later; function outerfunction() { var innervalue = 'alpaca'; function innerfunction() { console.log(outervalue); console.log(innervalue); later = innerfunction; outerfunction(); later(); function() innerfunction { function outerfunction var outervalue var innervalue var later

var later; Closure Example function outerfunction() { function innerfunction(paramvalue) { console.log(paramvalue); console.log(aftervalue); later = innerfunction; what will this print? var aftervalue = camel ; outerfunction(); later( alpaca );

var later; Closure Example function outerfunction() { function innerfunction(paramvalue) { console.log(paramvalue); console.log(aftervalue); prints: later = innerfunction; alpaca camel var aftervalue = camel ; outerfunction(); later( alpaca );

var later; Closure Example function outerfunction() { function innerfunction(paramvalue) { console.log(paramvalue); console.log(aftervalue); later = innerfunction; Closures include: Function parameters All variables in an outer scope var aftervalue = camel ; outerfunction(); later( alpaca ); declared after the function declaration!

PRIVATE VARIABLES var add = (function () { self-invoking var counter = 0; return function () {return counter += 1; )(); add();

PRIVATE VARIABLES function Llama() { private data member now! var spitted = false; this.spit = function() { spitted = true; this.hasspitted = function { return spitted;

CURRYING partial evaluation of functions function curriedadd(x){ return function(y){ return x+y; ; ; var addtwo = curriedadd(2); addtwo(3);

NEXT CLASS: JAVASCRIPT and the Web courses.engr.illinois.edu/cs498rk1/