Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | dawn-henson |
View: | 33 times |
Download: | 1 times |
Beginning JavaScript
4th Edition
Chapter 5
JavaScript—An Object-Based Language
Chapter 5 Objectives
What are JavaScript objects?
How do you create a JavaScript object?
What are some of the useful built-in JavaScript objects?
What are JavaScript objects?
A JavaScript object is a collection of properties and methods that are grouped together with a single name.
An object is used to gather together everything needed for a particular task.
How do you create a JavaScript object? var myArray = new Array();
- the new keyword tells JavaScript that you want to create a new object
- the Array() constructor function tells JavaScript what kind of object you want to create
- a reference to the new object is stored in a variable named myArray
Primitive Data
Primitive data, such as text and numbers, is the most basic data possible in JavaScript.
With primitive data, the variable holds the data’s actual value:
var myNumber = 23;
Reference Data
A variable assigned to an object holds a reference to the memory address where the data can be found, not the actual data itself.
var myArray = new Array(“Paul”, “Jeremy”, “Nick”);
Accessing an object’s properties
To access the value of a property of an object:
- name of the variable referencing the object- dot- name of the property
myArray.length
Calling an object’s methods
To call the methods of an object:
- name of the variable referencing the object- dot- name of the method
myArray.sort();
JavaScript Native Objects
String
Math
Array
Date
String Object
A String is just a series of individual characters.
Each character has a position, or index.
The first position or index is 0.
String Object
Properties and Methods:
- vast number of properties and methods
- most common and least complex are covered in this chapter
String Object
Property:
length – the number of characters in the string
var myName = new String(“Jeremy");document.write(myName.length);
String Object Method:
charCodeAt() – returns the Unicode character code for the character at a specific position in the string
var myName = new String("Paul");var firstChar = myName.charCodeAt(0);alert(firstChar);
String Object Method:
fromCharCodeAt() – converts Unicode character codes to characters to create a string
var myString;myString = String.fromCharCode(65,66,67);alert(myString);
String Object
Method:
indexOf() – finds the index position of a substring inside a string
var myString = "Paul";var found = myString.indexOf("a" );
String Object
Method:
lastIndexOf() – finds the index position of a substring inside a string
var myString = "Paul";var found = myString.lastIndexOf("u" );
String Object Methods:
substr() and substring()– both methods copy part of a string, but they differ in the parameters used
var myString = "JavaScript";var mySubString = myString.substring(0,4);
String Object Methods:
toLowerCase() and toUpperCase()– both methods return a string converted either to lowercase or to uppercase
var myString = "JAVASCRIPT";var myString = myString.toLowerCase();
Array Object
An array is an object that can contain more than one item of data at the same time
Array Object
Property:
length – the number of items in an array var myNumber = new Array(2,4);alert (myNumber.length);
Array Object Method:
concat() – concatenates two arrays var myNumber = new Array(2,4);var myNumberA = new Array(3,5);var concatArray = myNumber.concat(myNumberA);
Array Object Method:
slice() – copy part of an array var names = new Array("Paul","Sarah","Louise","Adam","Bob");var slicedArray = names.slice(1,3);
Array Object Method:
join() – convert an array to a string var names = new Array("Paul","Sarah","Louise","Adam","Bob");var nameList = names.join(,);
Array Object Method:
sort() – put array in ascending order var names = new Array("Paul","Sarah","Louise","Adam","Bob");names.sort();
Array Object Method:
reverse() – put array in descending order var names = new Array("Paul","Sarah","Louise","Adam","Bob");names.reverse();
Math Object
The Math object provides a number of useful mathematical functions and number manipulation methods.
Math Object
Property:
PI – the value of the mathematical constant PI (3.14159… and so on)
var myPi = Math.PI;alert (myPi);
Math Object
Method:
abs() – returns the absolute value of the number passed as a parameter
var myNumber = Math.abs(-2);alert (myNumber);
Math Object
Method:
ceil() – rounds up to the next largest number
var myNumber = Math.ceil(103.1);alert (myNumber);
Math Object
Method:
floor() – removes any numbers after a decimal point and then rounds down
var myNumber = Math.floor(103.1);alert (myNumber);
Math Object Method:
round() – rounds up if decimal part is 0.5 or greater, otherwise rounds down
var myNumber = Math.round(103.1);alert (myNumber);
Math Object
Summary of Rounding Methods:
Math Object Method:
random() – returns a random floating-point number in the range between 0 and 1
var myNumber = Math.random() * 7;alert (myNumber);
Math Object
Method:
pow() – raises a number to a specific power
var myNumber = Math.pow(2,4);alert (myNumber);
Date Object
The Date object handles everything to do with date and time in JavaScript.
Date Object
Creating a Date object:
var myDate = new Date();
Date Object
Getting Date Values:
Date Object
Setting Date Values:
Date Object Getting Time Values:
* getHours()
* getMinutes()
* getSeconds()
* getMilliseconds()
* toTimeString()
Date Object Setting Time Values:
* setHours()
* setMinutes()
* setSeconds()
* setMilliseconds()