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

Similar documents
CSC Web Programming. Introduction to JavaScript

Introduction to Web Tech and Programming

Client-Side Web Technologies. JavaScript Part I

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

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript: Objects, Methods, Prototypes

UNIT-III THE BASICS OF JAVASCRIPT

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

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Web Application Development

Chapter 4 Basics of JavaScript

CSCE 120: Learning To Code

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

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

Language Based isolation of Untrusted JavaScript

Computer Programming: C++

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Language Reference Manual simplicity

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

Javascript Arrays, Object & Functions

Chapter 6 Introduction to Defining Classes

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

15.1 Origins and Uses of Ruby

IBE101: Introduction to Information Architecture. Hans Fredrik Nordhaug 2008

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

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

More Perl. CS174 Chris Pollett Oct 25, 2006.

Full file at

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

JavaScript Functions, Objects and Array

CSCI-GA Scripting Languages

JavaScript I VP R 1. Copyright 2006 Haim Levkowitz. Outline

Santa s Reindeer Challenge - A solution using DMN and BPMN

CGS 3066: Spring 2015 JavaScript Reference

Chapter 2 Working with Data Types and Operators

RTL Reference 1. JVM. 2. Lexical Conventions

JavaScript Lecture 2

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

Assembler Language "Boot Camp" Part 2 - Instructions and Addressing. SHARE 118 in Atlanta Session March 12, 2012

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

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

Assembler Language "Boot Camp" Part 2 - Instructions and Addressing SHARE 116 in Anaheim February 28, 2011

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Decaf Language Reference Manual

YES User Guide Enrollment

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

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc.

JavaScript: Sort of a Big Deal,

A control expression must evaluate to a value that can be interpreted as true or false.

Building Java Programs

Pathologically Eclectic Rubbish Lister

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Absolute C++ Walter Savitch

C++ (Non for C Programmer) (BT307) 40 Hours

Chapter 3: Operators, Expressions and Type Conversion

The PCAT Programming Language Reference Manual

Continuing Education Course #192 Web-Based Programming For Engineers - Part 3

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

Getting to Know InfoTables

How to Work with a Reference Answer Set

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

Setting Up Your Public Profile

Query Studio Training Guide Cognos 8 February 2010 DRAFT. Arkansas Public School Computer Network 101 East Capitol, Suite 101 Little Rock, AR 72201

Using Lists (Arrays) Notes

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

Java Primer 1: Types, Classes and Operators

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Chapter 9. Subprograms

Software Design and Analysis for Engineers

JavaScript: More Syntax

Oracle Database: SQL and PL/SQL Fundamentals NEW

Node.js Training JavaScript. Richard richardrodger.com

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

4.1 Overview of JavaScript

Getting Started with Cisco Pulse

Such JavaScript Very Wow

COMS 3101 Programming Languages: Perl. Lecture 2

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Chapter 4 Defining Classes I

JScript Reference. Contents

Hands-On Perl Scripting and CGI Programming

PERL Scripting - Course Contents

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Review the Agenda with the participants

Sprite an animation manipulation language Language Reference Manual

Lesson Plan. Preparation

Oracle Database 11g: SQL and PL/SQL Fundamentals

Microsoft Visual Basic 2005: Reloaded

FRAC: Language Reference Manual

Computer Programming II Python

CSE 12 Week Eight, Lecture One

Draw a diagram of an empty circular queue and describe it to the reader.

Transcription:

IT 3203 Introduction to Web Development JavaScript II Labeling Systems October 7 Notice: This session is being recorded. Copyright 2007 by Bob Brown University Convocation Tuesday, October 13, 11:00 12:15 AM Student Center Theatre Convocation Speaker: Dr. John Palfrey Speaking on Born Digital in a Network Society Professor at Harvard Law School Vice Dean for Library and Information Resources Co-author of Born Digital: Understanding the First Generation of Digital Natives and also Access Denied: The Practice and Politics of Internet Filtering Creating Objects The new operator creates an object with no properties: var mycar = new Object(); mycar.make = 'Ford'; mycar.model = 'Crown Victoria'; Objects can be nested mycar.engine = new Object; mycar.engine.hp = 200; Accessing Property Values Two equivalent ways: prop1 = mycar.make; prop2 = mycar["make"]; Prefer the first form. Access to properties that don t exist returns an undefined value. Deleting a Property The delete keyword: delete mycar.make; What s in an Object? The for-in operator iterates over the properties of an object: for (var aproperty in mycar){ document.write ("Name: " + aproperty + "; Value: " + mycar[aproperty] + "<br />\n");

The Result of for-in Name: make; Value: Ford Name: model; Value: Crown Victoria Name: engine; Value: [object Object] Arrays Arrays are objects with special functionality. Array elements can be: Primitives Objects (including other arrays!) Arrays have dynamic length Creating Arrays There are two ways to create an array With a constructor: var bobsarray = new Array(1, 2, "three"); var billsarray = new Array(100); Implicitly, with an element list var bettysarray = [1, 2, "three"]; Characteristics of Arrays Subscripts start at zero. bobsarray[7] = 132; Arrays have a length property; the length is the highest subscript plus one. The length property is writable! bettysarray.length = 1234; But don t! Only assigned elements occupy space Array Methods tostring: Creates a single string that is the concatenation of all the elements in an array, with comma separators. join: Like tostring, but with a specified separator. reverse: Reverses the order of the elements sort: Sorts the elements alphabetically Concat Concatenates new elements onto an existing array and returns a new array var bobsarray=['1', '2', '3']; bettysarray=['6', '7']; megansarray=bobsarray.concat(bettysarray,'4'); Will be: 1, 2, 3, 6, 7, 4

Slice Returns a contiguous subset of an array bobsarray = new Array(2,4,6,8,10); 0 1 2 3 4 billsarray = bobsarray.slice(1,3) Will be: 4,6 A slice two elements long beginning at index 1. From the starting index, up to but not including the ending index. If there s no second parameter, the remainder of the array is copied. Push and Pop Add or remove elements at the end of an array. var reindeer=new Array('Dasher', 'Dancer', 'Donder', 'Blixem'); var lastdeer = reindeer.pop() lastdeer will be Blixem reindeer.push('rudolph'); Now Dasher, Dancer, Donder, Rudolph Donder is Dutch for thunder, so that other reindeer has to be named Blixem and not Blitzen. Shift and Unshift Add or remove elements at the beginning of an array; shift removes ( shifts out ) and unshift inserts. var reindeer=new Array('Dasher', 'Dancer', 'Donder', 'Blixem'); var firstdeer = reindeer.shift(); firstdeer is Dasher reindeer.unshift('rudolph'); Now Rudolph, Dancer, Donder, Blixem Multidimensional Arrays Implement multidimensional arrays as arrays of arrays. var threed = [[2,4,6], [1,3,5], [10,20,30]]; answer = threed[1][2]; answer becomes 5. Associative Arrays JavaScript arrays can use strings as subscripts to form an associative array, also called a hash. var ages = new Array(); ages["megan"] = 24; ages["bob"] = "sixty-harrummmph!" var niece_age = ages["megan"]; // becomes 24 var who = "Megan"; niece_age = ages[who]; // also 24 JavaScript Functions All JavaScript subprograms are functions. Defining a function: function name (params) {// body function myfunc(x) { rvalue = x * x; return rvalue; If no parameters needed, define with empty ( )

Functions and Statements If a function returns undefined, it is a stand-alone statement. Otherwise it can participate in expressions, including assignments a = b + myfunc(c); If no parameters are needed, call with empty ( ) Functions Must Be Defined Before Use Function definitions generally go in <head> Define functions before calling them: calls in <body> calls further down in <head> Local Variables var within a function definition creates a local variable. (Implicit definition does not.) Within the function, the local definition takes precedence over globals of the same name. var a=4, b=8; function myfunc() { var a = 0; a is local b = a; b is global Functions and Parameters Formal parameters in the function definition function myfunc(x, y) { x = x*x; return x + y/2; The two a s are not the same! The two b s are the same variable. Parameter Passing Formal parameters in the function definition Actual parameters in the function call function myfunc(x, y) {... z = myfunc(q, r); For primitives, formal parameters receive copies of actual parameters; call by value. For objects, formal parameters receive object references; call by name. Parameters and Side Effects Side effect: A function has a side-effect when it changes some variable outside itself. Generally, this is bad. Return a value instead. Side effects are impossible with primitive parameters, unless you modify a global variable. So, modifying global variables is generally bad. Side effects are possible and even likely when objects are passed as parameters.

The median Example Median: the middle of a list of elements odd: the value of the middle element even: mean of middle two One approach: use the sort method to sort the array. This has a side effect the array becomes sorted. Bad. Returning from a Function The return statement: returns to the caller passes a value (possibly undefined ) function sq(x) { return x*x; a = sq(2); // a becomes 4 If return is omitted, returns after last executable statement, passes back undefined Number and Type of Parameters JavaScript does no type checking of parameters (use typeof in your functions) JavaScript does not enforce correspondence between the number of formal parameters and the number of actual parameters. Too many actual parameters: excess ignored. Too few: the rest are undefined. The arguments[ ] Array A function s parameters are available in the arguments[ ] array. arguments.length is the number of parameters Can be used to write functions with: a variable number of parameters unnamed parameters Sort Revisited A caller to sort can supply a comparison function that understands the array contents. The function must return: a negative number if items already in order a positive number if a swap is needed zero if items are equal function num_order (a, b) { return a b; numberlist.sort(num_order) ; Reminder: Creating Objects The new operator creates an object with no properties. var mycar = new Object(); mycar.make = "Ford"; Objects can be nested mycar.engine = new Object(); mycar.engine.hp = 200;

JavaScript Constructors Constructors create and initialize properties of newlycreated objects. Every new expression must include a call to a constructor var mycar = new Object(); The constructor is named the same as the object being created. There are built-in constructors, e.g. Object Writing Constructors A constructor is a function, so you can write your own constructors. Constructor for a Car object: function Car (make, model, year) { this.make = make; this.model = model; this.year = year; this is a reference to the object being created var yourcar = new Car("Ford", "SVT", 2004); var mycar = new Car("Mazda", "RX-7", 1979); Writing Your Own Methods Methods are implemented as functions, so... write a function function display_car () {... then add function to the object in the constructor this.display = display_car; invoke the function through the object mycar.display(); Code for a Method function display_car() { for (var aproperty in this){ // Don't display methods if (typeof(this[aproperty])!= "function") { document.write (aproperty + "=" + this[aproperty] + "<br />\n"); // end of if // end of for // end of function display_car Methods in Constructors New constructor for a Car object: function Car (make, model, year) { this.make = make; this.model = model; this.year = year; this.display = display_car; The right side of the assignment is the name of the function, not a function call! More About Constructors and Objects Objects created by the same constructor have the same set of properties and methods, initially. properties can be added properties can be deleted There is no convenient way to determine whether two objects have the same set of properties or methods.

Labeling Systems Here is a Web Page Feedback in a conversation is immediate; Feedback when we converse via a Web site is at best delayed. What we call things (labels) are an important part of the conversation. Kinds of Labels Navigation system choices Contextual links Headings Index terms Labels Within Navigation Systems MIND & SPIRIT?!? Labels Within Navigation Systems Labels as Contextual Links

Labels as Headings Labels as Index Items About Iconic Labels How Jet Blue Fixed It Content Users Context Considerations in Designing Labels General Guidelines Narrow scope Labeling systems, not labels; consider: Style Presentation Syntax Granularity Comprehensiveness Audience

Sources of Labels Your current site Your competitors Controlled vocabularies, thesauri Search logs Creating Labeling Systems Content analysis Content authors Subject matter experts User advocates Users Usability testing Tuning and Tweaking Review the list Remove duplicates Resolve synonyms Review for consistency Plan ahead Questions