Tải bản đầy đủ (.ppt) (24 trang)

slide môn học HDJ bài 8 the core language objects in javascript

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (325.95 KB, 24 trang )

The Core language Objects in
JavaScript
Session 8


A Recap



Scripting Language – Scenarios Language
JavaScript’s Elements






Core JavaScript Object
Client-Side JavaScript Object
Server-Side JavaScript Object

JavaScript’s Tokens






Variables
Data Types
Operators (Arithmetic, Comparison, Logical, String, Evaluation)


Expression
Regular Expression
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 2 of 16


A Recap


Various message boxes






Function






Alert
Confirm
Prompt
Pre-defined functions
User-defined functions
Return statement

Display Information

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 3 of 16


Objectives


Work on Core Language Objects



Use object Attributes and Methods

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 4 of 16


Objects


The properties (variables) that define the
object and the methods (functions) that work
on the data are included in the object



For example, a car is an object. The
properties of the car are its make, model, and
color. They have some common methods like,
go (), brake(), reverse().
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 5 of 16



Properties and Methods


To access the properties of the object, we
must specify the object name and the
property:
objectName.propertyName



To access the methods of an object, we must
specify the object name and the required
method:
objectName.method ()
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 6 of 16


Example
function Car(model, type)
{
this.model = “Toyota”;
this.type = “for King Nothing”;

}

this.run = function()
{
document.write(“Brmm…”);
}


Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 7 of 16


Using objects


When creating a Web page we can insert:


Browser objects



Built in script language objects (vary depending
on the scripting language used)





HTML elements

We can also create our own objects for use
in the programs.
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 8 of 16


Object Hierarchy
Browser Objects


Script Objects

HTML Elements
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 9 of 16


The this statement


The this statement is more of an internal
property.



Its value indicates the current object. It can
have standard properties such as name,
length and value applied accordingly.

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 10 of 16


The for . . . in statement


The for . . in statement is used to cycle
through each property of an object or each
element of an array.




The syntax is:
for (variable in object)
{ statements; }
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 11 of 16


The with statement


The with statement is used to execute a set
of statements that have a specified object as
the reference.



The property is assigned to the object
specified in the with statement.



The syntax is:
with (object) { statements; }

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 12 of 16


New Operator






The new operator is used to create a new
instance of an object type
The object type may be user-define or built-in
objectName = new objectType (param1
[,param2] ...[,paramN])
where,
objectName is the name of the new object instance.
ObjectType is a function that defined the type of the object. For
example, Array.
Param[1, 2, . . ] are the property values of the object.
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 13 of 16


Eval function






The eval function is used to evaluate a string
of code without reference to any specific
object.
The string can be a JavaScript expression,
statement, or a group of statements
The expression can include variables and
properties of an object.

var x = 5;
var z = 10;
document.write(eval(“x + z + 5”));
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 14 of 16


String object






The string object is used to manipulate and
work with strings of text.
We can extract substrings and convert text to
upper- or lowercase characters in a program.
The general syntax is,
stringName.propertyName

or
stringName.methodName
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 15 of 16


Creating String object


There are 3 different methods of creating
strings.



Using the var statement

and optionally

assigning it to a value.


Using an assignment operator (=) with a
variable name.



Using the string () constructor.
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 16 of 16


String Object


Properties




length

Methods









charAt(index)
charCodeAt(index)
fromCharCode(icode, icode, icode…)
indexOf(findString, pos)
lastIndexOf(findString, pos)
Substring(start, end)
toLowerCase(), toUpperCase
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 17 of 16


Math Object


The Math object has properties and
methods that represent advanced
mathematical calculations.
function doCalc(x)
var a;
a = Math.PI * x
alert("The area
with a radius of "
a);
}


{
* x;
of a circle
+ x + “ is "

+

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 18 of 16


Math Object


Properties




PI

Methods







abs(number)

ceil(number)
floor(number)
round(number)
random()
pow(number, exp)
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 19 of 16


Date object


The Date built-in object contains both date
and time information.



The

Date

object

does

not

have

any


properties.


It has a large number of methods for setting,
getting, and manipulating dates.
Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 20 of 16


Date object


The Date object stores dates as the number
of

milliseconds

since

January

1,

1970,

00:00:00.
DateObject = new Date(parameters)

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 21 of 16



Date Object



Properties
Methods









getDate()
getDay()
getMonth()
getFullYear()
getHours()
getMinutes
getSeconds
getMiliseconds

setDate()
setMonth()
setFullYear()

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 22 of 16



Array Object


Properties




length

Methods






concat()
slice(start, end)
join()
sort()
reverse()

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 23 of 16


Timers



One Shot Timer




setTimeout(“javaScript code”, delay)

Timer that Fires at Regular Intervals



setInterval(“javaScript code”, interval)
clearInterval(handler)

Web Page Programming with HTML,DHTML & JavaScript/Session 8/ 24 of 16



×