Top Banner
More on Objects More on Objects
34

More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Jan 02, 2016

Download

Documents

Melinda Hardy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

More on ObjectsMore on Objects

Page 2: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

GoalsGoalsBy the end of this unit you should …… be able to identify selected String

properties… be able to identify and use selected

String methods… be able to identify selected Number

properties… be able to identify and use selected

Number methods… be able to identify and use methods

and properties of the Math object

Page 3: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Quick Review …Quick Review …What is an object?What is a primitive object?What is a compound object?What is a property?What is the difference between a

property and a property’s value?What is a method?What is an event?

Page 4: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Creating StringsCreating StringsThere are two ways to create Strings:

◦To create a string object, we could use a constructor:var userName = new String(“Waldo”);

◦Or, we could just assign a string value to a variable:var userName = “Waldo”;

I N341, I would like you to use constructor methods.

Page 5: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.length PropertyString.length PropertyThe String.length property can be used to

return the length of a string (number of characters used):

Syntax:String.length

Example:var myString = new String(“JavaScript”);var strLength = new Number(0);strLength = myString.length;

In the example above, the variable strLength gets the value of 10.

Page 6: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.charAtString.charAt(index)(index) MethodMethodThe String.charAt(index)

method will return the character at a given index in a string.

Indexes are ordinal numbers that indicate a position:0123456789 <= IndexJavaScript <= String

Page 7: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Example of Example of String.charAtString.charAt(index)(index)Example:var myString = new String(“JavaScript”);var returnChar1 = new String(“”);var returnChar2 = new String(“”);returnChar1 = myString.charAt(0);returnChar1 = myString.charAt(4);

returnChar1 gets the string value “J”returnChar2 gets the string value “S”

Page 8: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.indexOfString.indexOf(searchValue)(searchValue) MethodMethodThe String.indexOf(searchValue) method returns the index where a specified value begins in a given string

The method takes a string value for its argument and returns an integer value.

The method returns –1 if the term is not found.

Page 9: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Example of Example of String.indexOf(String.indexOf(substrsubstr))Example:var myString = new String(“JavaScript”);var search1 = new Number(0);var search2 = new Number(0);search1 = myString.indexOf(“Script”);search2 = myString.indexOf(“Visual Basic”);

search1 gets the integer value 4search2 gets the integer value -

1

Page 10: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.concat(String.concat(moreStringsmoreStrings) ) MethodMethodThe String.concat(moreStrings)

combines a string value with other string values to make a new String

Example:var str1 = new String(“JavaScript ”);var str2 = new String(“is ”);var str3 = new String(“cool.”);var str4 = new String(“”); str4 = str1.concat(str2, str3);

str4 gets the value “JavaScript is cool.”

Page 11: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.substr(String.substr(start, lengthstart, length) ) MethodMethodThe String.substr(start, length)

method returns a sub-string of characters beginning at a specified index (start) and returning a specified number of characters (length).

Example:var myString = new String(“JavaScript”);var rtnSub1 = new String(“”);var rtnSub2 = new String(“”);rtnSub1 = myString.substr(0,4);rtnSub2 = myString.substr(5,3);

returnSub1 would be given the value “Java” and returnSub2 would be given the value “cri”.

Page 12: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.substring(String.substring(index1, index1, index2index2) Method) MethodThe String.substring(index1, index2) method returns a sub-string of characters beginning at a specified index (index1) and ending at a specified index (index2).

Example:var myString = new String(“JavaScript”);var rtnSub = new String(“”);rtnSub = myString.substring(4,9);

returnSub gets the value “Script”.

Page 13: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

String.toUpperCase() &String.toUpperCase() &String.toLowerCase() MethodsString.toLowerCase() MethodsThe String.toUpperCase() and String.toLowerCase() methods convert a given string to upper- or lower-case.

Useful for non-case sensitive comparisons.Examples:var myString = new String(“JavaScript”);var rtnUpper = new String(“”);var rtnLower = new String(“”);rtnUpper = myString.toUpperCase();rtnLower = myString.toLowerCase();

rtnUpper gets the value “JAVASCRIPT” and rtnLower gets the value “javascript”.

Page 14: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Number TypesNumber TypesJavaScript doesn't distinguish

among the different number types. Although there are methods to convert to integer and to a floating point number, we create each using the same constructor:var myInteger = new Number(5150);

var myFloat = new Number(57.23);

Page 15: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Number.toFixed() MethodNumber.toFixed() Method

Number.toFixed(fractionalDigits) returns a number with a specified number of decimal places, after rounding the given value.

Rounding follows typical rounding rules:◦If a digit is >= 5, then we round up◦If a digit is < 5, then we round down

Page 16: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Number.toFixed() Method - Number.toFixed() Method - ExamplesExamplesExamples:var myNum1 = new Number(0.124);var myNum2 = new Number(0.126);var rtnNum1 = new Number(0);var rtnNum2 = new Number(0);rtnNum1 = myNum1.toFixed(2);rtnNum2 = myNum2.toFixed(2);

rtnNum1 gets the value 0.12 and rtnNum2 gets the value 0.13

Page 17: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Number.toString() MethodNumber.toString() Method

The Number.toString() converts a numeric value to a string type

Examples:var myNum = new Number(8675309);var rtnStr = new String(“”);rtnStr = myNum.toString();

rtnStr gets the value “8675309” (STRING type and NOT NUMBER type)

Page 18: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.PI PropertyMath.PI Property

The Math.PI property returns the constant value of Pi

You can use Math.PI for calculating the circumference or area of a circle

Examples:var radius = new Number(5);var circumference = new Number(0);circumference = 2*Math.PI*radius;

circumference gets the calculated circumference of a circle

Page 19: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.abs() MethodMath.abs() Method

The Math.abs() method returns the absolute value of a given value

Examples:var myNumber = new Number(-55);var absMyNumber = new Number(0);absMyNumber = Math.abs(myNumber);

absMyNumber gets the value 55

Page 20: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.ceil() MethodMath.ceil() Method

The Math.ceil() method returns the smallest integer greater than or equal to a number

Examples:var myNumber = new Number(45.67);var ceilMyNumber = new Number(0);ceilMyNumber = Math.ceil(myNumber);

ceilMyNumber gets the value 46

Page 21: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.floor() MethodMath.floor() Method

The Math.floor() method returns the largest integer less than or equal to a number

Examples:var myNumber = new Number(45.67);var floorMyNumber = new Number(0);floorMyNumber = Math.floor(myNumber);

floorMyNumber gets the value 45

Page 22: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.max(a, b) MethodMath.max(a, b) Method

The Math.max(a, b) method returns the largest number, given a set of two numbers (a, b)

Examples:var x = new Number(14);var y = new Number(8);var myMax = Math.max(x, y);

myMax gets the value 14

Page 23: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.min(a, b) MethodMath.min(a, b) Method

The Math.min(a, b) method returns the smallest number, given a set of two numbers (a, b)

Examples:var x = new Number(14);var y = new Number(8);var myMin = Math.min(x, y);

myMin gets the value 8

Page 24: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.pow(Math.pow(base, exponentbase, exponent) ) MethodMethodThe Math.pow(base, exponent)

method returns the result of an exponential expression, given a base and an exponent

Examples:var x = new Number(2);var y = new Number(3);var result = Math.pow(x, y);

result gets the value 8

Page 25: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.random() MethodMath.random() Method

The Math.random() method returns a “random” number between 0 and 1

Examples:var myRan = new Number(0); myRan = Math.random(); //returns .4678myRan = myRan*100; //scale the numbermyRan = Math.Round(myRan);

If Math.random() returns .4678, myRan is multiplied by 100 (46.78) and then rounded (47)

Page 26: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Math.sqrt(x) MethodMath.sqrt(x) Method

The Math.sqrt(x) returns the square root of a number, given any number

Examples:var x = new Number(49);var result = new Number(0);result = Math.sqrt(x);

result gets the value 7

Page 27: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Modal WindowsModal WindowsA modal window is a small action

window that forces a user to respond to it before continuing in an application.

Typical examples of modal windows include the “Save As” window, a window confirming an exit, error windows, etc.

In JavaScript, we can call on some methods of the window object to produce modal windows.

Page 28: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

window.alert() Methodwindow.alert() MethodThe window.alert() method is

a way to produce a modal output window.

The window.alert() method takes a single string variable or a single string literal for an argument. The string represents the message we wish to output to the user.

Page 29: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

window.alert() Exampleswindow.alert() ExamplesExample 1:var msgToOutput = new String(“”);msgToOutput = “Hello World!”;window.alert(msgToOutput);

Example 2:window.alert(“Hello World!”);

Page 30: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

window.prompt() Methodwindow.prompt() MethodThe window.prompt() method is a

way to produce a modal input window. The window.prompt() method takes

two string variables or a string literals (or a combination of both) for arguments. The first string represents the question we want to ask the user. The second is help text that will appear in line where the user is to type their answer.

The window.prompt() method always returns a string value!

Page 31: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

window.prompt() Examplewindow.prompt() Example Example 1:var txtQuestion = new String(“”);var txtUserName = new String(“”);var txtDefault = new String(“”);txtQuestion = “What is your name?”;txtDefault = “TYPE YOUR ANSWER HERE.”;txtUserName = window.prompt(txtQuestion, txtDefault);

Page 32: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

For More Information …For More Information …See the Core JavaScript

Reference:http://www.croczilla.com/~alex/reference/javascript_ref/

Page 33: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

Questions?Questions?

Page 34: More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.

ResourcesResourcesJavaScript: The Definitive Guide,

Fourth Edition by David Flanagan (O’Reilly, 2002)