JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Advertisements

JavaScript Arrays Arrays allow us to store lists of items Arrays are created with square brackets:  var a = []; // an empty array  var b = [10]; // an.
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Using Data Types (No Audio Component) © Dr. David C. Gibbs
1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
CS0004: Introduction to Programming Variables – Numbers.
Scripting Languages.
UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS.
Introduction to Python
JavaScript Lecture 6 Rachel A Ober
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Working with background, images and date object Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Javascript core objects. Review question Which of the following is TRUE concerning a variable declared globally? a. The variable cannot be accessed anywhere.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
ALBERT WAVERING BOBBY SENG. Week 5: More JavaScript  Quiz  Announcements/questions.
Arrays, date, random. Declared Variables Explicitly define them Can give them starting values Figures out type Case sensitive var x = 5; (note: this is.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Sahar Mosleh California State University San MarcosPage 1 JavaScript An Object-Based Language.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
The Grammar of JavaScript.  Each line of a script is a statement  An instruction that JavaScript can evaluate (make sense of)  Ends with a semicolon;
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.
1 JavaScript
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
資訊工程學系 3A 彭博涵 2011/03/26 1.  什麼是 JavaScript?  JS Basic  JS Object 2.
Arrays. Lesson Objectives  To understand what an array is and it’s function  To know how code and array in VB.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
CSD 340 (Blum)1 Arrays See Beginning JavaScript pp
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Sahar Mosleh California State University San MarcosPage 1 Object Base Programming.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
Algorithms and Computer Programming. Algorithms algorithm is an ordered sequence of instructions for solving a problem. Look at it as a building blocks.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Tutorial 11 1 JavaScript Operators and Expressions.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Chapter 9. An event-driven, object-based programming language that provides various types of functionality to pages. Object based: it is a language that.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Agenda  Unit 8: Programs and Data. 2 Structured data - Array There are many situations where we want to apply the same processing to a collections.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
OCR AS Level F452: Data types and data structures Data types and data structures a. define different data types, eg numeric (integer, real),
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
JavaScript Non Primitive Datatype
CGS 3066: Web Programming and Design Spring 2017
SEEM4570 Tutorial 05: JavaScript as OOP
Java Script.
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
Arrays in C.
Agenda Unit 8: Programs and Data.
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
Objects as Variables Featuring the Date Object
In Class Program: Today in History
Tutorial 4 JavaScript as OOP Li Xu
Presentation transcript:

JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating on the built-in objects in this class 1

2 JavaScript Objects JavaScript is an object-based language Everything is an object – Objects have methods – Objects have properties (variables) In JavaScript you are programming using objects

3 Objects By Example Real world Obect: A Ford Explorer Properties – Car is blue, has 4 wheels, travelling at 75mph Methods – gas is delivered to the engine, air is delivered to the horn Events – press the gas pedal, beep the horn (the author incorrectly identifies these as methods)

4 Methods Methods are like functions, they DO something – when called from an event – when called from another method Can receive information – shiftingGear( ) method would receive "which gear to shift to" data Can return information – howMuchGasLeft( ) might return 9.4 gallons

5 Properties Some properties can not be changed – body shape (unless you have an accident!) – number of wheels Properties that can change – amount of gas in the tank – speed at which you're travelling

6 Slacker Definition An object is – a bunch methods and a bunch of variables attached to a name

7 Built-in Objects Date Object – getTime( ), getHours( ), getMinutes( ) Array Objects – length( ) method to find out the number of elements in the array – sort( ) method - sorts elements into alphabetical order Math Objects String Objects

8 Creating a Standard Object var myArray = new Array( ); – very much like Java itself! "new" is the keyword that tells JavaScript to create a new object of a certain "type" The "Array( )" part call the "constructor" for an Array object

9 Passing Data to the Constructor var myArray = new Arrray( ); var myArray = new Array("Paul", "Paula", "Pauline"); var myDate = new Date( ); var myDate = new Date("1 Jan 2000");

10 Object Properties Write the object name, following by a dot (period), followed by the variable name – e.g. myArray.length number of elements contained in the array – could store it in a variable var myVar = myArray.length Some properties can be updated Some can not

11 Object Methods Some return data – Date.gethours( ) returns the number of hours Some just perform a function – Array.sort( ) sorts an array in place

12 JavaScript Native Objects String, Math, Date, and Array Example creating an Object – var string1 = new String("Hello"); var string2 = new String(123); var string1 = "Hello"; (JavaScript works it out for you)

13 Just a String A string is just a series of individual characters and that each character has a position or index, somewhat like arrays – Unfortunately, the first position is zero, not one

14 The length Property The number of characters in a string var myname = new String("Paul"); document.write(myName.length);

Exercise 2.33 Write a JavaScript program that calls a function that prompts the user for their name and returns the length of their name Display … "Your (name) has (number) characters" 15

Array Objects – Properties length – Methods sort reverse push pop 16

Arrays Conceptually A named list Instead of naming each variable in a list… – member1, member2, etc. Name the list and use a number index… – members(1), members(2), etc. 17

Declare an Array var members = new Array( ); Then populate the list… – members[0] = "Steve"; members[1] = "Jane"; Or… – var members = new Array("Steve", "Jane"); 18

The length Property Returns the number of elements in the array – var names = new Array( ); names[0] = "Paul"; names[1] = "Catherine"; names[2] = "Steve"; document.write("The last name is " + names[names.length - 1]; 19

The sort( ) Method Puts elements in an Array in ASCIIbetical order Remember JavaScript is case sensitive – var names = new Array ("Paul", "Mary", "Steve"); var i;# Author uses element index names.sort( ); for (i = 0; i )"; } 20

Exercise 2.7 Write while-loop that prompts the user to enter their name. Add their names to an array If they enter 'exit', then end the prompting Sort the array and display the list in sorted order 21

Math Objects Covered in Last Lesson – Properties PI – Methods round random 22

Exercise 2.34 Write a JavaScript program that calls a function that returns a random integer between 1 and 100, inclusive, and display that on the webpage like so… "The number I'm thinking of is (number)" 23

Date Objects Handles everything to do with dates, times, and timers in JavaScript – Can find out the date and time now – Can store dates – Do calculations with dates – Convert dates into Strings 24

Creating a Date Object Four ways to create a data object – var theDate1 = new Date( ); – var theDate2 = new Date( )' the number of milliseconds since 1/1/1970 – var theDate3 = new Date("31 January 2000"); – var theDate4 = new Date(2000,0,31,15,35,20,20); 0 is Janurary, 11 is December (...go figure) 25

Date Methods getDate( )The day of the month getDay( )The day of the week as an integer getMonth( )The month as an integer getFullYear( )The year as a four digit number 26

Setting Date Values setDate( ) setMonth( ) setFullYear( ) – example: myDateObject.setFullYear(2000); myDateObject.setDate(27); myDateObject.setMonth(1); Note: There is no setDay( ) method, the day is calculated by the values of the other fields 27

Date Error Example var myDate = new Date("1 Jan 2000"); myDate.setDate(32); document.write(myDate); 32 is not a valid day in January, so JavaScript calulates 32 days from Jan 1st, hence the date is not Feb 1st! 28

Date Calculation var nowDate = new Date( ); var currentDay = nowDate.getDate( ); nowDate.setDate(currentDay + 28); Adds 28 days to the date 29

Getting/Setting Time Values getHours( )setHours( ) getMinutes( )setMinutes( ) getSeconds( )setSeconds( ) getMilliseconds( )setMilliseconds( ) 30

Exercise 2.35 Create a JavaScript program that calls the function that displays today's date in the following format… "This is the 17th day of October in the year of 2009" 31

End 32