JavaScript: Objects, Methods, Prototypes

Similar documents
JavaScript: Objects, Methods, Prototypes

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

MVC: Model View Controller

LECTURE-3. Exceptions JS Events. CS3101: Programming Languages: Javascript Ramana Isukapalli

HTML User Interface Controls. Interactive HTML user interfaces. Document Object Model (DOM)

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

CSC Web Programming. JavaScript Browser Objects

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

Javascript. Many examples from Kyle Simpson: Scope and Closures

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

Client vs Server Scripting

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

JavaScript Introduction

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

CICS 515 a Internet Programming Week 3. Mike Feeley

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

Q1. What is JavaScript?

CSC Web Programming. Introduction to JavaScript

Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

Events: another simple example

INDEX SYMBOLS See also

Ruby: Introduction, Basics

Part 1: jquery & History of DOM Scripting

Fundamentals of Website Development

DOM Primer Part 2. Contents

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

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

Web Application Development

JavaScript: Events, the DOM Tree, jquery and Timing

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Interactor Tree. Edith Law & Mike Terry

Ruby: Introduction, Basics

Ruby: Introduction, Basics

JavaScript: Introduction, Types

JavaScript Handling Events Page 1

JavaScript: Sort of a Big Deal,

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

CSC Javascript

IS 242 Web Application Development I

CE212 Web Application Programming Part 2

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

Photo from DOM

Web Site Development with HTML/JavaScrip

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

CSS: Cascading Style Sheets

Session 6. JavaScript Part 1. Reading

CGS 3066: Spring 2015 JavaScript Reference

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Web Development & Design Foundations with HTML5

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

JAVASCRIPT BASICS. Handling Events In JavaScript. In programing, event-driven programming could be a programming

Such JavaScript Very Wow

Notes on JavaScript (aka ECMAScript) and the DOM

Session 16. JavaScript Part 1. Reading

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

A Balanced Introduction to Computer Science, 3/E

AJAX: The Basics CISC 282 March 25, 2014

Introduction to DHTML

Sections and Articles

University Convocation IT 3203 Introduction to Web Development Creating Objects Accessing Property Values

dhtml2010.pdf February 25,

Boot Camp JavaScript Sioux, March 31, 2011

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

Beijing , China. Keywords: Web system, XSS vulnerability, Filtering mechanisms, Vulnerability scanning.

Web Engineering (Lecture 06) JavaScript part 2

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

The first sample. What is JavaScript?

JavaScript. What s wrong with JavaScript?

CSE 154 LECTURE 10: THE DOM TREE

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

AJAX: The Basics CISC 282 November 22, 2017

An Brief Introduction to a web browser's Document Object Model (DOM) Lecture 20 COMP110 Spring 2018

JavaScript CS 4640 Programming Languages for Web Applications

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

JAVASCRIPT FOR PROGRAMMERS

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

JavaScript: Coercion, Functions, Arrays

Acknowledgments. Who Should Read This Book?...xxiv How to Read This Book...xxiv What s in This Book?...xxv Have Fun!...xxvi

Recall: Document Object Model (DOM)

CSS The web browser uses its own resources, and eases the burden on the server. It has fewer features than server side scripting.

JQUERYUI - WIDGET FACTORY

Web Programming/Scripting: JavaScript

<form>. input elements. </form>

JavaScript for C# Programmers Kevin

JavaScript and the DOM MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

Comprehensive AngularJS Programming (5 Days)

classjs Documentation

Lecture 8: JavaScript

Session 7. JavaScript Part 2. W3C DOM Reading and Reference

Chapter 6 Introduction to Defining Classes

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

Javascript Lecture 23

EVENT-DRIVEN PROGRAMMING

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

! 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

Transcription:

JavaScript: Objects, Methods, Prototypes Computer Science and Engineering College of Engineering The Ohio State University Lecture 14

What is an Object? Property: a key/value pair (aka "name"/ value) Object: a partial map of properties Keys must be unique Creating an object, literal notation var mycar = { make: "Acura", year: 1996, plate: "NKR462" }; To access/modify an object's properties: mycar.make = "Ford" mycar["year"] = 2006; var str = "ate"; mycar["pl"+str] = "NKR463";

Arrays vs Associative Arrays 0 4 age 4 1 "hi" greeting "hi" 2 doors 3 3.14 0 true pi 3.14 0 true 1 true 1 true 2 false 2 false

Dynamic Size, Just Like Arrays Objects can grow mycar.state = "OH"; //4 properties var mybus = {}; mybus.driver = true; //adds a prop mybus.windows = [2, 2, 2, 2]; Objects can shrink delete mycar.plate; //mycar is now {make: "Ford", // year: 2006, state: "OH"}

Testing Presence of Key Boolean operator: in propertyname in object Evaluates to true iff object has the indicated property key "make" in mycar //=>true "speedometer" in mycar //=>false "OH" in mycar //=>false

Iterating Over Properties Iterate using for in syntax for (property in object) { object[property] } Notice [] to access each property for (p in mycar) { document.write(p +": " + mycar[p]); }

Methods The value of a property can be: A primitive (boolean, number, string, null ) An object, an array, or a function var temp = function(sound) { play(sound); return 0; } mycar.honk = temp; More succinctly: mycar.honk = function(sound) { play(sound); return 0; }

Example: Method var mycar = { make: "Acura", year: 1996, plate: "NKR462", honk: function(sound) { play(sound); return 0; } };

Object Properties mycar make year plate honk() "Acura" 1996 "NKR462" play(sound); return 0;

Keyword "this" in Functions Recall distinguished parameter x.f(y,z); //x is the distinguished param. Inside a function, keyword "this" function report() { return this.plate + this.year; } At run-time, "this" is set to the distinguished parameter of invocation mycar = {plate: "NKR462", year: 1996}; yourcar = {plate: 340, year: 2013}; mycar.register = report; yourcar.info = report; mycar.register(); //=>"NKR4621996" yourcar.info(); //=>2353

Object Properties mycar yourcar plate "NKR462" plate 340 year 1996 year 2013 register() info() report() return this.plate + this.year;

Constructors Any function can be a constructor When calling a function with "new": 1. Make a brand new (empty) object 2. Call the function, with the new object as the distinguished parameter 3. Implicitly return the new object to caller A "constructor" often adds properties to the new object simply by assigning them f u n c t i o n D o g ( n a m e ) { this.name = name; //adds 1 property //no explicit return } v a r f u r B a l l = new Dog("Rex"); Naming convention: Functions intended to be constructors are capitalized

Example function Circle (x, y, radius) { this.centerx = x; this.centery = y; this.radius = radius; this.area = function() { return Math.PI * this.radius * this.radius; } } var c = new Circle(10, 12, 2.45);

Creating a Circle Object var c = new Circle(10, 12, 2.45); Circle() this.centerx = x; this.centery = y;... Etc...

Creating a Circle Object var c = new Circle(10, 12, 2.45); Circle() this.centerx = x; this.centery = y;... Etc...

Creating a Circle Object var c = new Circle(10, 12, 2.45); Circle() centerx centery radius 10 12 2.45 this.centerx = x; this.centery = y;... Etc... area() return Math.PI * this.radius * this.radius

Creating a Circle Object c var c = new Circle(10, 12, 2.45); Circle() centerx centery radius 10 12 2.45 this.centerx = x; this.centery = y;... Etc... area() return Math.PI * this.radius * this.radius

Creating a Circle Object c var c = new Circle(10, 12, 2.45); Circle() centerx centery radius 10 12 2.45 this.centerx = x; this.centery = y;... Etc... area() return Math.PI * this.radius * this.radius

Prototypes Every object has a prototype A hidden, indirect property ([[Prototype]]) What is a prototype? Just another object! Like any other! When accessing a property (ie obj.p) First look for p in obj If not found, look for p in obj's prototype If not found, look for p in that object's prototype! And so on, until reaching the basic system object

Prototype Chaining greeting "hi" doors age 4 tostring() pi 3.14 hasownproperty() 0 true push() 1 true pop() 2 false etc

Class-Based Inheritance interfaces extends implements classes extends static static static instantiates objects

Example Consider two objects var dog = {name: "Rex", age: 3}; var pet = {color: "blue"}; Assume pet is dog's prototype //dog.name is "Rex" //dog.color is "blue" (follow chain) pet.color = "brown"; //dog.color is "brown" (prop changed) dog.color = "green"; //pet.color is still "brown" (hiding)

Delegation to Prototype dog pet name "Rex" color "brown" age 3 color "green"

Prototypes Are Dynamic Too Prototypes can add/remove properties Changes are felt by all children //dog is {name: "Rex", age: 3} //dog.mood & pet.mood are undefined pet.mood = "happy"; //add to pet //dog.mood is now "happy" too pet.bark = function() { return this.name + " is " + this.mood; } dog.bark(); //=>"Rex is happy" pet.bark(); //=>"undefined is happy"

Delegation to Prototype dog pet dog.bark(); pet.bark(); name "Rex" color "brown" age 3 mood bark() "happy" return this.name + " is " + this.mood;

Connecting Objects & Prototypes How does an object get a prototype? var c = new Circle(); Answer 1.Every function has a prototype property Do not confuse with hidden [[Prototype]]! 2.Object's prototype link (ie [[Prototype]]) is set to the function's prototype property When a function Foo is used as a constructor (i.e., new Foo), the value of foo's prototype property is the prototype object of the created object

Prototypes And Constructors c centerx centery 10 12 area() constructor radius 2.45 Circle() prototype this.centerx = x; this.centery = y;... Etc...

Idiom: Methods in Prototype function Dog(n, a) { this.name = n; this.age = a; }; var Canine = { bark: function (sound) { return this.name + "says" + sound; } }; Dog.prototype = Canine;

Idiom: Methods in Prototype function Dog(n, a) { this.name = n; this.age = a; }; var Canine = { bark: function (sound) { return this.name + "says" + sound; } }; Dog.prototype = Canine;

Idiom: Methods in Prototype function Dog(n, a) { this.name = n; this.age = a; }; Dog.prototype = { bark: function (sound) { return this.name + "says" + sound; } }; //sets prototype to new anonymous object

Idiom: Methods in Prototype function Dog(n, a) { this.name = n; this.age = a; }; Dog.prototype.bark = function (sound) { }; return this.name + "says" + sound; //better: extends existing prototype

Idiom: Classical Inheritance function Animal() {... }; function Dog() {... }; Dog.prototype = new Animal(); //create prototype for future dogs Dog.prototype.constructor = Dog; //set prototype's constructor properly //(ie should point to Dog() )

Setting up Prototype Chains new Dog() new Animal() name "Rex" constructor prototype constructor Dog() prototype Animal()

Summary Objects as associative arrays Partial maps from keys to values Can dynamically add/remove properties Can iterate over properties Method = function-valued property Keyword this for distinguished parameter Constructor = any function Prototypes are "parent" objects Delegation up the chain of prototypes Prototype is determined by constructor Prototypes can be modified

Objects are Everywhere Global variables in JavaScript are a lie Implicitly part of some global object, provided by execution environment See FireBug DOM view In JSFiddle: document.write(this + "<br/>"); for (p in this) { document.write(p + ": " + this[p] + "<br/>"); }

Window Object For JavaScript running in a browser, implicit global object is window Many properties, including location (url of displayed document) status (text in status bar of browser) history innerheight, innerwidth alert(), prompt() document (tree of displayed document)

Document is a Tree head html lang: en body element attr name: attr value text title meta charset: utf-8 p Hello a href: planet.html! br img src: pic.png alt: a globe Something Short and Sweet World

DOM: "Document Object Model" DOM is a language-neutral API for working with HTML (and XML) documents Different programming languages have different bindings to this API But very closely linked to JavaScript In JavaScript, tree nodes! objects A tree element with attributes <input type="text" name="address"> A JavaScript object with many properties { tagname: "INPUT", type: "text", name: "address", /*lots more*/ }

DOM History Ad hoc DOM existed from the beginning of JavaScript Core purpose of client-side execution: Enable user interaction with the document Need a connection between programming language (JavaScript) and the document DOM 1 specification (W3C) in '98 Standardized mapping tree!objects and functions for modifying the tree DOM 2 ('00): added styles and event handling DOM 3 ('04): fancier tree traversal & indexing schemes

Simplest Mapping window document write: outputs text to document body forms: array of forms in a page elements[]: array of widgets in a form anchors: all the anchors in document links: all the links in the document getelementbyid(string): finds a node etc

Document is a Tree head html lang: en body element attr name: attr value text title meta charset: utf-8 p Hello a href: planet.html! br img src: pic.png alt: a globe Something Short and Sweet World

Node is a JavaScript Object Properties parentnode, childnodes, firstchild, lastchild, nextsibling, previoussibling innerhtml tagname XML lower case ("a"), HTML upper case ("A") attributes, name, id, class style Hyphenated properties in CSS (background-color) Become camelcase in JavaScript (backgroundcolor) Methods appendchild(node), removechild(node), insertbefore(node) hasattribute(attr), removeattribute(attr), getattribute(attr), setattribute(attr) getelementsbytagname(name)

How to Find a Node in Tree 1. Hard coding with "flat" techniques Eg children arrays document.forms[0].elements[0] Downside: too brittle If the document structure changes a little, everything breaks 2. Using an element's name attribute In HTML: <form name="address"> <input name="zip"... /> </form> In JavaScript: document.address.zip Downside: direct path still hard coded

How to Find a Node in Tree 3. To get a unique element: document method getelementbyid In HTML <td id="shipping">...</td> In JavaScript document.getelementbyid("shipping") Downside: every element you want to find needs unique ID 4. Combination: element ID for form, arrays for options in selection element

Example <form id="wheels"> <input type="checkbox" name="vehicles" value="car" /> Car <input type="checkbox" name="vehicles" value="truck" /> Truck <input type="checkbox" name="vehicles" value="bike" /> Bike </form> var numchecked = 0; var elt = document.getelementbyid("wheels"); for (i = 0; i < elt.vehicles.length; i++) { if (elt.vehicles[i].checked) numchecked++; }

Interactive Documents To make a document interactive, you need: Widgets (ie HTML elements) Buttons, windows, menus, etc. Events Mouse clicked, window closed, button clicked, etc. Event listeners Listen (ie wait) for events to be triggered, and then perform actions to handle them

Events Drive the Flow of Control This style is event driven programming Event handling occurs as a loop: Program is idle User performs an action Eg moves the mouse, clicks a button, types in a text box, selects an item from menu, This action generates an event (object) That event is sent to the program, which responds Code executes, could update document Program returns to being idle

Handling Events Mechanism Three parts of the event-handling mechanism Event source: the widget with which the user interacts Event object: encapsulated information about the occurred event Event listener: a function that is called when an event occurs, and responds to the event HTML Element event object ahandler()

Programmer Tasks Define an event handler Any function can be an event handler Often need information about the triggering event in order to know what response is needed Register handler with source element Detect event and invoke handler Ha! Just kidding, you do NOT do this

Simple Example: Color Swaps <p>this page illustrates changing colors</p> <form> <p> <label> background: <input type="text" name="back" size="10" onchange="foo('bg', this.value)" /> </label> <br /> <label> foreground: <input type="text" name="fore" size="10" onchange="foo('fg', this.value)" /> </label> </p> </form>

Color Swaps (JavaScript) function foo(place, color) { if (place === "bg") document.body.style.backgroundcolor = color; else document.body.style.color = color; }

Event Propagation Elements are nested in tree When an event occurs, which element's handler(s) is(are) notified? First, propagation path is calculated: from root to smallest element Then event dispatch occurs in 3 phases 1. Capture (going down the path) 2. Target (smallest element) 3. Bubble (going up the path, reverse of 1)

http://www.w3.org/tr/dom-level-3-events/

Bubbling Up Usually, handling is done in phase 2 and 3 Example: mouse click on hyperlink Handler for <a> element displays a pop-up ("Are you sure you want to leave?") Once that is dismissed, event flows up to enclosing <p> element, then <div> then etc until it arrives at root element of DOM This root element (ie window) has a handler that loads the new document in the current window

Programmer Tasks Define a handler Easy, any function will do Register handler Multiple ways to link (HTML) tree elements with (JavaScript) functions Be triggered by the event Ha! Still kidding Get information about triggering event Multiple (incompatible) ways for handler to get the event object

Registering an Event Handler Three techniques, ordered from: Oldest (most brittle, most universal) to Newest (most general, least standard) 1. Inline (link in HTML itself) <a href="page.html" onclick="foo()"> 2. Direct (link in JavaScript) var e = //find source element in tree e.onclick = foo; 3. Chained (In JavaScript, browser differences) var e = //find source element in tree e.addeventlistener("click", foo, false);

Inline Registration (pre DOM) HTML attributes, vary by element type For window: onload, onresize, onunload, Forms & elements: onchange, onblur, onfocus, onsubmit, Mouse events: onclick, onmouseover, onmouseout, Keyboard events: onkeypress, onkeyup, The value of these attributes is JavaScript code to be executed Normally just a function invocation Example <a href="page.html" onclick="foo()"> Advantage: Quick, easy, universal Disadvantage: mixes code with content

Direct Registration (DOM 1) Use properties of DOM element objects onchange, onblur, onfocus, onclick, onmouseover, onmouseout, onkeypress, onkeyup, Set this property to appropriate handler var e = //find source element in tree e.onclick = foo; Note: no parentheses! e.onclick = foo(); //what does this do? Disadvantage? No arguments to handler Not a problem, handler gets event object Real disadvantage: 1 handler/element

Example var x = document.getelementsbytagname("div"); for (var i = 0; i < x.length; i++) { } x[i].onmouseover = function () { } this.style.backgroundcolor="red" x[i].onmouseout = function () { } this.style.backgroundcolor="blue"

Chained Registration (DOM 2) Each element has a collection of handlers Add/remove handler to this collection var e = //find source element in tree e.addeventlistener("click", foo, false); Note: no "on" in event names (ie "click") Third parameter: true for capture phase Disadvantage: browser incompatibilities e.addeventlistener() //FF, Webkit, IE9+ e.attachevent() //IE5-8 Many browser compatibility issues with DOM and events Solution: Libraries Eg jquery, Dojo, Prototype, YUI, MooTools,

Example var x = document.getelementsbytagname("div"); for (var i = 0; i < x.length; i++) { } x[i].addeventlistener ("click", function () { }, this.act = this.act false; this.act =!this.act; this.style.backgroundcolor = (this.act? "red" : "gray"); false);

Task: Getting Event Object Most browsers: parameter to handler function myhandler(event) IE: event is property of window Common old-school idiom: function myhandler(event) { event = event window.event; etc Again, libraries are the most robust way to deal with these issues

Summary DOM: Document Object Model Programmatic way to use document tree Get, create, delete, and modify nodes Event-driven programming Source: element in HTML (a node in DOM) Handler: JavaScript function Registration: in-line, direct, chained Event is available to handler for inspection