Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Special Topic JSON. Introducing JSON JSON (JavaScript ObjectNotation): A data format based on the object literal format Advantage of JSON over XML – JSON.
The Web Warrior Guide to Web Design Technologies
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
What we learned in last lesson How to add JavaScript to your HTML pages How to use dialog boxes and variables to store and utilize user input How to write.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
11/1/06 1 Hofstra University, CSC005 Chapter 8 (Part 3) High Level Programming Languages.
Chapter 7: Sub and Function Procedures
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Introduction to JavaScript for Python Programmers
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
4.1 JavaScript Introduction
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
Generations of Programming Languages First generation  Machine Language Second Generation  Assembly Language Third Generation  Procedural language such.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
Chapter 3 : Processing on the Front End JavaScript Technically its name is ECMA-262, which refers to the international standard which defines it. The standard.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
>> Introduction to JavaScript
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
Functions and Objects. First Midterm exam Date: 10/10/2006 (Tuesday) Content: Multiple choices Determine results of the code Write codes Covert everything.
Functions and Objects. First Midterm exam Date: October 8, 2008 Content: Two parts: On-paper:Multiple choices On-computer: Write codes Cover everything.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
JavaScript, Fourth Edition
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Using Client-Side Scripts to Enhance Web Applications 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Objects.
JavaScript Shopping Cart Yong Choi CSU Bakersfield.
Document Objects Forms, Images and Links. The document object model Each element of an HTML document, such as an image, form, link, or button, can be.
Introducing JavaScript. Goals By the end of this lecture you should … Be able to describe the differences among object methods, object properties and.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Objects.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript” –The name was changed when Java became.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
PeopleCode and the Component Processor
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Programming Web Pages with JavaScript
Barb Ericson Georgia Institute of Technology May 2006
Event Driven Programming & User Defined Functions
Introducing JavaScript
Presentation transcript:

Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives are their properties and verbs are their methods. Types of objects -User defined objects. -Core or built-in objects (Date, String, Number) -Browser and Document objects.

Dot Syntax A dot is used to separate the objects when descending the hierarchical tree-like structure used to describe all of the components of an object. Constructor A constructor is a method that creates an instance of an object. JavaScript comes with several built-in constructors. The new keyword precedes the name of the constructor that will be used to create the object. Examples of constructors: Object(), Array(), Date().

Object() The Object() constructor returns a reference to an object. The return value is assigned to a variable. Example The Object() Constructor var pet = new Object(); alert(pet);

Properties of the Object Any object subordinate to another object is also a property of that object. The properties assigned to the object are not variables and are not defined with the var keyword. Example The Object() Constructor var pet = new Object(); alert(pet); pet.cat = new Object(); alert(pet.cat); pet.cat.name = "Sneaky"; pet.cat.color = "yellow"; pet.cat.size = "fat"; pet.cat.attitude = "stuckup";

Methods of the Object Methods are special functions that are used to describe how object behaves or acts: pet.cat.play(); // no parameters pet.dog.fetch(ball); // passing parameters Format var myobj = new Object();

Example User-defined objects var toy = new Object(); // Create the object toy.name = "Lego"; // Assign properties to the object toy.color = "red"; toy.shape = "rectangle"; document.write(" The toy is a " + toy.name + "."); document.write(" It is a " + toy.color + " " + toy.shape+ ".");

Creating an Object with a User-Defined Function You can create a function that specifies the object’s name, properties, and methods. The function serves as a template or prototype of an object. The this keyword is used to refer to the object that has been passed to a function.

Example User-defined objects function book(title, author, publisher){ // Defining properties this.title = title; this.author = author; this.publisher = publisher; } var myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall"); document.writeln(" " + myBook.title + " " + myBook.author + " " + myBook.publisher);

Defining methods for an Object Simple Methods function distance(r, t){ // define the object this.rate = r; // assign properties this.time = t; } function calc_distance(){ // define a function that will be used as a method return this.rate * this.time; } var speed=eval(prompt("What was your speed (miles per hour)? ","")); var elapsed=eval(prompt("How long did the trip take (hours)?","")); var howfar=new distance(speed, elapsed); // call the constructor howfar.distance=calc_distance; // create a new property var d = howfar.distance(); // invoke method alert("The distance is " + d + " miles.");

A Method defined in a constructor User-defined objects function book(title, author, publisher){ // Receiving parameters this.pagenumber=0; // Properties this.title = title; this.author = author; this.publisher = publisher; this.uppage = pageForward; // Assign function name to a property this.backpage = pageBackward; } function pageForward(){ // Functions to be used as methods this.pagenumber++; return this.pagenumber; } function pageBackward(){ this.pagenumber--; return this.pagenumber; }

A Method defined in a constructor (continued) var myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall" ); //Create new object myBook.pagenumber=5; document.write( " "+ myBook.title + " " + myBook.author + " " + myBook.publisher + " Current page is " + myBook.pagenumber ); document.write(" Page forward: " ); for(i=0;i<3;i++){ document.write(" " + myBook.uppage()); // Move forward a page } document.write(" Page backward: "); for(;i>0; i--){ document.write(" " + myBook.backpage()); // Move back a page }

Properties can be objects Properties Can be Objects var pet = new Object(); // pet is an object pet.cat = new Object(); // cat is a property of the pet; object.cat is also an object pet.cat.name="Sylvester"; // cat is assigned properties pet.cat.color="black"; pet.dog = new Object(); pet.dog.breed = "Shepherd"; pet.dog.name = "Lassie"; document.write(" The cat's name is " + pet.cat.name + "."); document.write(" The dog's name is " + pet.dog.name + ".");

Object Literals Object created by assigning it a comma- separated list of properties enclosed in curly braces. FORMAT var object = {property1: value, property2: value};

Example Object Literals var car = { make: "Honda", year: 2002, price: 30000, owner: "Henry Lee"}; var details=car.make + " "; details += car.year + " "; details += car.price + " "; details += car.owner + " "; document.write(details);

With keyword The with keyword is used for referencing an object’s properties or methods Example The with Keyword function book(title, author, publisher){ this.title = title; // properties this.author = author; this.publisher = publisher; this.show = display; // Define a method } function display(anybook){ with(this){ // The with keyword var info = "The title is " + title; info += "\nThe author is " + author; info += "\nThe publisher is " + publisher; alert(info); }

Example (continued) var childbook = new book("A Child's Garden of Verses", "Robert Lewis Stevenson", "Little Brown"); var adultbook = new book("War and Peace", "Leo Tolstoy", "Penguin Books"); childbook.show(childbook); // call method for child's book adultbook.show(adultbook); // call method for adult’s book

The for/in loop can be used to iterate through a list of object properties or array elements FORMAT for (var property_name in object){ statements }

Example User-defined objects function book(title, author, publisher){ this.title = title; this.author = author; this.publisher = publisher; this.show=showProps; // define a method for the object } function showProps(obj, name){ // function to show the object's properties var result = ""; for (var prop in obj){ result += name + "." + prop + " = " + obj[prop] + " "; } return result; } myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall"); document.write(" " + myBook.show(myBook, "myBook"));

prototypes OO languages support a feature called inheritance, where an object can inherit the properties of another. You can add properties to objects after they have been created by using the prototype objects. In OO, the object’s data describes the properties. The object, along with its properties and methods, is bundled up into a container called a class. Each JavaScript class has a prototype object and one set of properties.

Example User-defined objects and Inheritance function Book(title, author, publisher){ // The Book class this.title = title; this.author = author; this.publisher = publisher; this.show=showProps; } function showProps(obj,name){ var result = ""; for (var i in obj){ result += name + "." + i + " = " + obj[i] + " "; } return result; }

Example (continued) // Add a new function function lastEdition(){ this.latest=prompt("Enter the latest edition for "+this.title,""); return (this.latest); } // Add a new property with prototype Book.prototype.edition=lastEdition; var myBook=new Book("JavaScript by Example", "Ellie", "Prentice Hall"); // Define a new method document.write(" " + myBook.show(myBook,"myBook")+" "); document.write("The latest edition is "+ myBook.edition()+" ");

Extending a JavaScript Object Since all objects have the prototype object, it is possible to extend the properties of a JavaScript built-in object, just as we did for a user-defined object.

Example Prototypes // Customize String Functions function uc(){ var str=this.big(); return( str.toUpperCase()); } function lc(){ var str=this.small(); return( str.toLowerCase()); } String.prototype.bigUpper=uc; String.prototype.smallLower=lc; var string="This Is a Test STRING."; string=string.bigUpper(); document.write(string+" "); document.write( string.bigUpper()+" "); document.write(string.smallLower()+" ");

The Name Grabber Program Name Grabber function copyName() { var userName = document.myForm.txtName.value; var greeting = "Hi there, "; greeting += userName; greeting += "!"; document.myForm.txtGreeting.value = greeting; }

The Name Grabber Program Name Grabber Please type your name: <input type = "text" name = "txtName"> <input type = "button" value = "click me" onClick = 'copyName()'> <input type = "text" name = "txtGreeting">

var userName = document.myForm.txtName.value; The net result of this line is to copy the value of txtName to the userName variable. Document.myForm.txtName.value essentially becomes a variable that you can assign values to and from, just like any other variable. Once you understand this, the following line makes perfect sense as well: document.myForm.txtGreeting.value = greeting;