Вы находитесь на странице: 1из 17

Lecture

JavaScript An Object Based


Language
What are Objects?
What is Object in real world?
Cars, Computers, Tables etc
How to define a Car?
Blue Car, 150 km/h speed, Manual
These are properties of the car e-g colour = blue
How to use a Car?
Turn ignition key, press the pedal, beep the horn etc
These are the methods of the car object
Some time methods use information, sometime
provide information
Some time methods change the properties value
Car is defined by collection of methods and properties
Why JavaScript Objects
Primitive data
Not too Complex and easy to deal with
But all information is not simple e-g time calculation
Is there any easier way to deal with complex
information like times and dates?
Objects can be used in this situation
Use Date objects which have variety of properties
and methods
getTime(), getHour(), getMinutes() etc
Number of objects are available in JavaScript

Using JavaScript Objects
Creating an Object
var myVariable = new ConstructorName(opt parameters);
Examples
var myDate = new Date();
var myDate = new Date(1 Jan 2010);
How data stores
With primitives, variable stores the actual values
With object, variable stores references to the memory
address where data is actually stored.

Using JavaScript Objects
Using an Objects Property
ObjectVariable.PropertyName
Example: myArray.length;
Some properties are read-only
Calling an Objects Method
ObjectVariable.MethodName(Opt Parameters)
Some methods return a value
Some methods do a task
Example:
myData.getMinutes(); (Return minutes)
myArray.sort() (Sort the array)

Primitives vs Objects
There are String, Number and Boolean objects
corresponding to String, Number and Boolean data
types
Example:
var myString = new String(Im a String object);
var lengthOfString = myString.length;
But if we have
var mySecondString = Im a primitive string;
How to find the length of this string?
JavaScript temporarily convert this primitive string to
string Object just for the operation

JavaScript Native Objects
String Object Methods
Searching String within another string:
indexOf() , lastIndexOf()
Take two parameters (String to search, Starting Position)
Copying part of a String
subStr() and subString()
Takes two parameters (start Postion, Last Position/
Number of characters)
Last parameter is optional

JavaScript Native Objects
Converting Cases of characters:
toLowerCase() , toUpperCase()
Return a string in desired case
Selecting a single character from a string
charAt() and charCodeAt()
Takes two parameters (start Postion, Last Position/
Number of characters)
Last parameter is optional
Converting character codes to a string
fromCharCode()
Takes coma separated numbers as character codes
Return a string on the basis of code passed

Array Object
Finding out how many elements in an array
length property
myArray.length
Joining Arrays
concat() method return the union of two arrays
thirdArray = firstArray.concat(secondArray);
firstArray = firstArray.concat(secondArray);
Copying part of an Array
slice() method return an array depending on parameters
Take two parameters startIndex, lastIndex(optional)


Array Object
Converting an Array to a single string
join() method return a string of the elements of the array
thirdString = myArray.join(@);
Parameter is optional, if not given comma will be inserted

Putting an Array in order / reverse order
sort() make the order of elements decending/acending
(alphabatically/ numerically)
reverse() put the array elements in reverse order




The Math Object
Provide a number of useful mathematical number
manipulation methods
No need to define a variable as math object, JavaScript
automatically create them
Accessing math object properties and call methods
math.propertyName ---->> math.PI
Math.MethodName() ---->> math.abs(parametr)

abs(number) return the absolute value of number




The Math Object
Finding the largest and smallest number
Math.max(n1, n2) return largest number among n1 and n2
Math.min(n1, n2) return smallest number among n1 and n2
Math.random() method
Return a random floating-point number between 0 and 1
0 is included and 1 is not included
Math.pow(n, p)
Take two parameters the number and specified power to raise
the number
Return a number equal to n^p



The Math Object
Rounding numbers
Math.ceil(n) round the n up to the next whole number
Math.floor(n) remove decimal point and return whole number
Math.round(n) round up or round down depending on number
Summary of rounding methods



The Date Object
Can be used to
Find the current date and time
Store your own dates and time
Do calculations with these dates
Converts the dates into strings
Creating Date object
var myDate = new Date() {current date and time}
Date() can take number milliseconds since 1 Jan 1970 at
00:00:00 GMT
Var myDate = new Date(1395278000000);
Set the date Mar 20 2014 06:13:20 GMT




The Date Object
Getting Date values
Following methods can be used with any date object to get
values of different properties of your date object




The Date Object
Setting Date values
Following methods can be used with any date object to set
the values within your date object




Examples
myDateObject.setFullYear(2009);
myDateObject.setDate(27);
myDateObject.setMonth(1);




The Date Object
Getting Time values
getHours()
getMinutes()
getSeconds()
getMilliSeconds()
Setting the Time values
setHours()
setMinutes()
setSeconds()
setMilliSeconds();

Вам также может понравиться