Vous êtes sur la page 1sur 6

JSSomeBuiltinObjects

Note:manyofthecontentsofthispagearetakenfromw3schoolwebsite.

JavaScriptStringObject
TheStringobjectisusedtomanipulateastoredpieceoftext.

Examplesofuse:

ThefollowingexampleusesthelengthpropertyoftheStringobjecttofindthelengthofastring:
vartxt="Helloworld!"
document.write(txt.length)

Thecodeabovewillresultinthefollowingoutput:
12

ThefollowingexampleusesthetoUpperCase()methodoftheStringobjecttoconvertastringto
uppercaseletters:
vartxt="Helloworld!"
document.write(txt.toUpperCase())

Thecodeabovewillresultinthefollowingoutput:
HELLOWORLD!

CompleteStringObjectReference

ForacompletereferenceofallthepropertiesandmethodsthatcanbeusedwiththeStringobject,goto
ourcompleteStringobjectreference.

JavaScriptDateObject
DefiningDates

TheDateobjectisusedtoworkwithdatesandtimes.

WedefineaDateobjectwiththenewkeyword.ThefollowingcodelinedefinesaDateobjectcalled
myDate:
varmyDate=newDate()

Note:TheDateobjectwillautomaticallyholdthecurrentdateandtimeasitsinitialvalue!

ManipulateDates
WecaneasilymanipulatethedatebyusingthemethodsavailablefortheDateobject.

IntheexamplebelowwesetaDateobjecttoaspecificdate(14thJanuary2010):
varmyDate=newDate()
myDate.setFullYear(2010,0,14)

AndinthefollowingexamplewesetaDateobjecttobe5daysintothefuture:
varmyDate=newDate()
myDate.setDate(myDate.getDate()+5)

Note:Ifaddingfivedaystoadateshiftsthemonthoryear,thechangesarehandledautomaticallybythe
Dateobjectitself!

ComparingDates
TheDateobjectisalsousedtocomparetwodates.

Thefollowingexamplecomparestoday'sdatewiththe14thJanuary2010:
varmyDate=newDate()
myDate.setFullYear(2010,0,14)
vartoday=newDate()
if(myDate>today)
alert("Todayisbefore14thJanuary2010")
else
alert("Todayisafter14thJanuary2010")

CompleteDateObjectReference

ForacompletereferenceofallthepropertiesandmethodsthatcanbeusedwiththeDateobject,goto
ourcompleteDateobjectreference.

JavaScriptArrayObject
DefiningArrays
TheArrayobjectisusedtostoreasetofvaluesinasinglevariablename.

WedefineanArrayobjectwiththenewkeyword.ThefollowingcodelinedefinesanArrayobjectcalled
myArray:
varmyArray=newArray()

Therearetwowaysofaddingvaluestoanarray(youcanaddasmanyvaluesasyouneedtodefineas
manyvariablesyourequire).

1:
varmycars=newArray()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

Youcouldalsopassanintegerargumenttocontrolthearray'ssize:
varmycars=newArray(3)
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

2:
varmycars=newArray("Saab","Volvo","BMW")

Note:Ifyouspecifynumbersortrue/falsevaluesinsidethearraythenthetypeofvariableswillbe
numericorBooleaninsteadofstring.

AccessingArrays
Youcanrefertoaparticularelementinanarraybyreferringtothenameofthearrayandtheindex
number.Theindexnumberstartsat0.

Thefollowingcodeline:
document.write(mycars[0])

willresultinthefollowingoutput:
Saab

ModifyValuesinExistingArrays

Tomodifyavalueinanexistingarray,justaddanewvaluetothearraywithaspecifiedindexnumber:
mycars[0]="Opel"

Now,thefollowingcodeline:
document.write(mycars[0])

willresultinthefollowingoutput:
Opel

CompleteArrayObjectReference
ForacompletereferenceofallthepropertiesandmethodsthatcanbeusedwiththeArrayobject,goto
ourcompleteArrayobjectreference.

JavaScriptBooleanObject
TheBooleanobjectisanobjectwrapperforaBooleanvalue.
TheBooleanobjectisusedtoconvertanonBooleanvaluetoaBooleanvalue(trueorfalse).

WedefineaBooleanobjectwiththenewkeyword.ThefollowingcodelinedefinesaBooleanobject
calledmyBoolean:
varmyBoolean=newBoolean()

Note:IftheBooleanobjecthasnoinitialvalueorifitis0,0,null,"",false,undefined,orNaN,the
objectissettofalse.Otherwiseitistrue(evenwiththestring"false")!

AllthefollowinglinesofcodecreateBooleanobjectswithaninitialvalueoffalse:
varmyBoolean=newBoolean()
varmyBoolean=newBoolean(0)
varmyBoolean=newBoolean(null)
varmyBoolean=newBoolean("")
varmyBoolean=newBoolean(false)
varmyBoolean=newBoolean(NaN)

AndallthefollowinglinesofcodecreateBooleanobjectswithaninitialvalueoftrue:
varmyBoolean=newBoolean(true)
varmyBoolean=newBoolean("true")
varmyBoolean=newBoolean("false")
varmyBoolean=newBoolean("Richard")

CompleteBooleanObjectReference
ForacompletereferenceofallthepropertiesandmethodsthatcanbeusedwiththeBooleanobject,go
toourcompleteBooleanobjectreference.

JavaScriptMathObject
TheMathobjectallowsyoutoperformcommonmathematicaltasks.

TheMathobjectincludesseveralmathematicalvaluesandfunctions.YoudonotneedtodefinetheMath
objectbeforeusingit.

MathematicalValues

JavaScriptprovideseightmathematicalvalues(constants)thatcanbeaccessedfromtheMathobject.
Theseare:E,PI,squarerootof2,squarerootof1/2,naturallogof2,naturallogof10,base2logofE,
andbase10logofE.

YoumayreferencethesevaluesfromyourJavaScriptlikethis:
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E

MathematicalMethods
InadditiontothemathematicalvaluesthatcanbeaccessedfromtheMathobjecttherearealsoseveral
functions(methods)available.

Examplesoffunctions(methods):

Thefollowingexampleusestheround()methodoftheMathobjecttoroundanumbertothenearest
integer:
document.write(Math.round(4.7))

Thecodeabovewillresultinthefollowingoutput:
5

Thefollowingexampleusestherandom()methodoftheMathobjecttoreturnarandomnumberbetween
0and1:
document.write(Math.random())

Thecodeabovecanresultinthefollowingoutput:
0.13251054050929345

Thefollowingexampleusesthefloor()andrandom()methodsoftheMathobjecttoreturnarandom
numberbetween0and10:
document.write(Math.floor(Math.random()*11))

Thecodeabovecanresultinthefollowingoutput:
3

CompleteMathObjectReference

ForacompletereferenceofallthepropertiesandmethodsthatcanbeusedwiththeMathobject,goto
ourcompleteMathobjectreference.

HTMLDOMObjects
TheHTMLDOMisaW3CstandardanditisanabbreviationfortheDocumentObjectModelfor
HTML.

TheHTMLDOMdefinesastandardsetofobjectsforHTML,andastandardwaytoaccessand
manipulateHTMLdocuments.

AllHTMLelements,alongwiththeircontainingtextandattributes,canbeaccessedthroughtheDOM.
Thecontentscanbemodifiedordeleted,andnewelementscanbecreated.
TheHTMLDOMisplatformandlanguageindependent.Itcanbeusedbyanyprogramminglanguage
likeJava,JavaScript,andVBScript.

FollowthelinksbelowtolearnmoreabouthowtoaccessandmanipulateeachDOMobjectwith
JavaScript:

Object Description
Document RepresentstheentireHTMLdocumentandcanbeusedtoaccessallelements
inapage
Anchor Representsan<a>element
Area Representsan<area>elementinsideanimagemap
Base Representsa<base>element
Body Representsthe<body>element
Button Representsa<button>element
Event Representsthestateofanevent
Form Representsa<form>element
Frame Representsa<frame>element
Frameset Representsa<frameset>element
Iframe Representsan<iframe>element
Image Representsan<img>element
Inputbutton RepresentsabuttoninanHTMLform
Inputcheckbox RepresentsacheckboxinanHTMLform
Inputfile RepresentsafileuploadinanHTMLform
Inputhidden RepresentsahiddenfieldinanHTMLform
Inputpassword RepresentsapasswordfieldinanHTMLform
Inputradio RepresentsaradiobuttoninanHTMLform
Inputreset RepresentsaresetbuttoninanHTMLform
Inputsubmit RepresentsasubmitbuttoninanHTMLform
Inputtext RepresentsatextinputfieldinanHTMLform
Link Representsa<link>element
Meta Representsa<meta>element
Option Representsan<option>element
Select RepresentsaselectionlistinanHTMLform
Style Representsanindividualstylestatement
Table Representsa<table>element
TableData Representsa<td>element
TableRow Representsa<tr>element
Textarea Representsa<textarea>element

2007MehmudAbliz

Vous aimerez peut-être aussi