JavaScript Built-In Objects
JavaScript Built-In Objects
a- Will print Hello World in bold letters b- Error in Line 2 c- Error in line 3
JavaScript Objects
1. Custom Objects
• Objects that you, as a JavaScript developer, create and use.
2. Built – in Objects
• Objects that are provided with JavaScript to make your life as a
JavaScript developer easier.
String RegExp
Number Error
Object
Array
Date
Math
Boolean
String Object
Example:
<script>
var colorArray = new Array();
colorArray [0]=“red”;
colorArray [1]=“blue”;
colorArray [2]=“green;
//OR
var colorArray = new Array(“red”,”blue”,”green”);
//OR
var colorArray=[];
var colorArray=[“red”,”blue”,”green”];
</script>
o Syntax:
<script>
var assocArray = new Array( );
assocArray[“Ahmed"] = “Excellent";
assocArray[“Tareq"] = “pass";
</script>
Literal object
What is an Object?
o An object is an unordered list of primitive data types (and sometimes
reference data types) that is stored as a series of name-value pairs.
o Each item in the list is called a property (functions are called methods).
// using object
//you don’t need to create instance
emp1. Name=“Mahmoud”;
. alert(emp1.Name);
Literal object (Cont.)
o Syntax:
<script>
var d = new Date(); // holds current date
var d = new Date(dateString); // Ex. "October 13, 2014 11:13:00"
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
</script>
Date Object (Cont.)
Date Object Number Conventions:
1. get" methods
for getting date and time values from date objects
2. "set" methods
for setting date and time values in date objects
3. "to" methods
for returning string values from date objects.
Date Object (Cont.)
1. get Methods
var myDate= new Date ( "November 25,2006 11:13:55");
Name Example
setDate(number) someDate.setDate(25)
setHours(number) someDate.setHours(14)
setMinutes(number) someDate.setMinutes(50)
setMonth(number) someDate.setMonth(7)
setSeconds(number) someDate.setSeconds(7)
setTime(TimeString) someDate.setTime(myDate.getTime())
setFullYear(number) someDate.setFullYear(88)
Date Object (Cont.)
3. to Methods
var myDate = new Date ( "November 25,2006 11:13:00");
Name Example Returned Value
toUTCString() myDate.toUTCString() Sat, 25 Nov 2006 09:13:00 UTC
ص11:13:002006, نوفمبر 25
toLocaleString() myDate.toLocaleString() (Based on date format in your
OS)
myDate.toLocaleTimeStri
toLocaleTimeString() ص11:13:00
ng()
myDate.toLocaleDateStri
toLocaleDateString() 2006,
نوفمبر 01
ng()
Sat Nov 25 11:13:00
toString() myDate.toString()
UTC+0200 2006
toDateString() myDate.toDateString() Sun Nov 1 2006
Mode/Flag:
o Optional parameter, indicates the mode in which the
Regular Expression is to be used:
(i) ignore case.
(g) global search.
(m) Multiline
<
/d (any digit)
() (group)
Regular expressions(Cont.)
Regular Expression Object properties:
o global:
– If this property is false, which is the default, the search stops when
the first match is found. Set this to true if you want all matches.
o ignoreCase:
– Case sensitive match or not, defaults to false.
o multiline:
– Search matches that may span over more than one line, defaults
to false.
o lastIndex:
– The position at which to start the search, defaults to 0.
o source:
– Contains the regexp pattern.
Regular expressions(Cont.)
Regular Expression Object Methods:
o test()
– returns a boolean (true when there's a match, false otherwise)
– Example:
var reg=/j.*t/ ;
var t= reg.test("Javascript")
false case sensitive
o exec()
– returns first matched strings.
– Example:
Var reg=/j.*t/ i;
Var str=“Jscript is the same of javascript”;
var res= reg.exec(str);
Regular expressions(Cont.)
String Methods that Accept Regular Expressions as
Parameters :
o match()
- returns an array of matches.
o search()
- returns the position of the first match.
o replace()
- allows you to substitute matched text with another string.
document.write(myStr.replace(/e/,”?”));
//L?t's see what happens!
document.write(myStr.replace(/e/g,”?”));
//L?t's s?? what happ?ns!
o split()
- also accepts a RegExp when splitting a string into array elements.
Regular expressions(Cont.)
RegExp Object patterns Reference:
o http://www.w3schools.com/jsref/jsref_obj_regexp.asp
o http://regexlib.com/CheatSheet.aspx
RegExp Library:
o http://www.regxlib.com/
o Implicitly:
thrown using the throw statement.
Error Object(Cont.)
Error Object Properties:
Property Description
description Plain-language description of error
fileName URI of the file containing the script throwing the
error
lineNumber Source code line number of error
message Plain-language description of error (ECMA)
name Error type (ECMA)
number Microsoft proprietary error number
Error Object(Cont.)
Error constructor:
– var e = new Error();
Six additional Error constructor ones exist and they all inherit Error:
Error Object(Cont.)
Error Object standard Properties:
• Name: The name of the error constructor used to create the
object
• Example:
var e = new Error('Oops');
<script>document.writeln(“Thank
You!”)</script>