CNIT 133 Interactive Web Pags – JavaScript and AJAX Objects.

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

Java Script Session1 INTRODUCTION.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Julian on JavaScript: Objects Julian M Bucknall, CTO.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Using Data Types (No Audio Component) © Dr. David C. Gibbs
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
JavaScript, Third Edition
OBJECTS MIT-AITI copyright Introduction Object and Properties Constructors and Methods Prototypes and Inheritance Object-Oriented JavaScript Objects.
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.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
CNIT 133 Interactive Web Pags – JavaScript and AJAX Expression.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chapter 3: Data Types and Operators JavaScript - Introductory.
JavaScript, Fourth Edition
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX How to run JavaScript?
Built-in Data Structures in Python An Introduction.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Variables.
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.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
JavaScript and Ajax (Control Structures) Week 4 Web site:
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
CNIT 133 Interactive Web Pags – JavaScript and AJAX Popup Boxes.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CGS 3066: Web Programming and Design Spring 2017
Chapter 4 Client-Side Programming: the JavaScript Language
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Fundamentals
JavaScript: Functions.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
JavaScript and Ajax (Expression and Operators)
ARRAYS MIT-AITI Copyright 2001.
T. Jumana Abu Shmais – AOU - Riyadh
Modern JavaScript Develop And Design
CS5220 Advanced Topics in Web Programming JavaScript Basics
Defining Classes and Methods
JavaScript CS 4640 Programming Languages for Web Applications
Javascript Chapter 19 and 20 5/3/2019.
CS3220 Web and Internet Programming JavaScript Basics
CGS 3066: Web Programming and Design Spring 2016
Lecture 9: JavaScript Syntax
Reasoning with Types.
Presentation transcript:

CNIT 133 Interactive Web Pags – JavaScript and AJAX Objects

Agenda My Web Site: (download syllabus, class notes). Objects and Arrays. Creating objects. Object properties. Enumerating properties. Checking and deleting properties. Objects as associative arrays. Constructor property. toString() and valueOf() methods.

Objects and Arrays Objects and arrays are two fundamental data types in JavaScript. They are collections of values. An object is a collection of named values. An array is a specialized kind of object that behaves as an ordered collection of numbered values.

Creating Objects An object is an unordered collection of properties, each of which has a name and a value. The named values may be primitive values (number or string or boolean), or may be another objects. The easiest way to create an object is to include an object literal in your JavaScript code. An object literal is a comma-separated list of property name/value pairs, enclosed within curly braces: var empty = { }; // an object with no properties var point = { x:0, y:0 }; // x or "x" are the same here var circle = { x:point.x, y:point.y+1, radius:2 }; var homer = {"name":"homer simpson", "age": 34, "married": true, "occupation": "plant operator", " ": The new operator can create specialized kinds of objects: var a = new Array(); // create an empty array var d = new Date();// create an object with current date & time It is also possible to define your own constructor functions.

Object Properties You normally use the. to access the value of an object’s properties The value on the left of the. should be the object whose property you want to access (the name of the variable that contains the object reference or expression that evaluates to an object) The value on the right of the. should be the name of the property (must be an identifier, not a string or an expression) You can create a new property of an object simply by assigning a value to it: var book = {};// create a new object with empty object literal book.title = "JavaScript: the definitive Guide"; book.chapter1 = new Object(); book.chapter1.title = "Introduction to JavaScript"; book.chapter1.pages = 11; book.chapter2 = { title: "Lexical Structure", pages: 6}; alert("Outline: " + book.title + "\n\t" + "Chapter 1 " + book.chapter1.title + "\n\t" + "Chapter 2 " + book.chapter2.title);

Enumerating Properties The for/in loop provides a way to loop through, or enumerate, the properties of an object This can be useful when working with objects that may have arbitrary properties whose names you do not know in advance: function displaypropnames(obj) { var names = ""; for(var nm in obj) { names += nm + "\n"; } alert(names); } NOTE: the for/in loop does not enumerate properties in any specific order, and it does not enumerate certain predefined properties or methods

Checking Property Existence The in operator can be used to test for the existence of a property. The left side of this operator should be the name of the property as a string. The right side should be the object to be tested. If o has a property named “x”, then 1 will assign to “x”. if ("x" in o) { o.x = 1; }

Deleting Properties You can use the delete operator to delete a property of an object: delete book.chapter2; Note that deleting a property does not merely set the property to undefined; it actually removes the property from the object. After deletion, for/in will not enumerate the property And the in operator will not detect it.

Objects as Associative Arrays The. operator is used to access the properties of an object. It is possible to use the [ ] operator, which is more commonly used with arrays, to access these properties. The following two JavaScritp expression have the same value: object.property object["property"] The first statement’s property name is an identifier The second statement’s property name is a string When you use the. operator to access a property of an object, the name of the property is expressed as an identifier. Identifiers must be typed literally; they are not a datatype, so they cannot be manipulated by the program. When you access a property of an object with the [ ] array notation, the name of the property is expressed as a string. Strings are JavaScript datatypes, so they can be manipulated and created while a program is running: var customer = {address0: "addr0", address1: "addr1", address2: "addr2"}; var addr = ""; for (var i = 0; i < 3; i++) { addr += customer["address" + i] + "\n"; } alert(addr); This code reads and concatenates the address0, address1, … properties of the customer object. When an object is used in this fashion, it is often called an associative array – a data structure that allows you to dynamically associate arbitrary values with arbitrary strings. The term “map” is often used to describe this situation as well: JavaScript objects map strings (property names) to values.

Universal Object Properties and Methods All objects in JavaScript inherit from the Object class. All objects also support the properties and methods defined by Object.

The constructor property In JavaScript, every object has a constructor property that refers to the constructor function that initializes the object. var d = new Date(); d.constructor == Date;// evaluates to true The constructor property can help determine the type of an object: if ((typeof o == "object") && (o.constructor == Date)) // then do something with the Date object The instanceof operator checks the value of the constructor property, so the code could be written: if ((typeof o == "object") && (o instanceof Date)) // then do something with the Date object

The toString() Method The toString( ) method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked. JavaScript invokes this method of an object whenever it needs to convert the object to a string (e.g. use in + operator, alert() method) When an array is converted to a string, you obtain a list of the array elements, each converted to a string. When a function is converted to a string, you obtain the source code for the function.

The valueOf() Method The valueOf( ) method is called when JavaScript needs to convert an object to some primitive type other than a string – typically, a number. JavaScript calls this method automatically if an object is used in a context where a primitive value is required.