Kratak Pregled Java Script-A: Operatori
Kratak Pregled Java Script-A: Operatori
OPERATORI
Modulus (%)
var1 % var2
12 % 5 = 2.
Increment (++)
var++
or ++var
y = x++
y=++x
Decrement (--)
var--
or --var
y = x-y=--x
Na nivou bita
Operator
Usage
Description
Bitwise AND
a&b
Bitwise OR
a|b
Bitwise XOR
a^b
Returns a one in a bit position if bits of one but not both operands
are one.
Bitwise NOT
~a
Left shift
Sign-propagating
right shift
Zero-fill right shift a >>> Shifts a in binary representation b bits to the right, discarding bits
b
shifted off, and shifting in zeros from the left.
Logiki operatori
Operator
Usage
Description
Returns true if both logical expressions expr1 and expr2 are true.
Otherwise, returns false.
or (||)
expr1 || expr2 Returns true if either logical expression expr1 or expr2 is true. If both
are false, returns false.
not (!)
!expr
x += y
x=x+y
x -= y
x=x-y
x *= y
x=x*y
x /= y
x=x/y
x %= y
x=x%y
x <<= y
x = x << y
x >>= y
x = x >> y
x >>>= y x = x >>> y
x &= y
x=x&y
x ^= y
x=x^y
x |= y
x=x|y
Operatori poreenja
Operator
Description
Example
Equal (= =)
Returns true if the operands are not equal. x != y returns true if x is not
equal to y.
Returns true if left operand is greater than x > y returns true if x is greater
right operand.
than y.
Greater than or
equal (>=)
Returns true if left operand is greater than x >= y returns true if x is greater
or equal to right operand.
than or equal to y.
Less than or equal Returns true if left operand is less than or x <= y returns true if x is less
(<=)
than or equal to y.
KONTROLE TOKA
if (condition) {
statements1
} else {
statements2
}
Primer:
function checkData () {
if (document.form1.threeChar.value.length == 3) {
return true
} else {
alert("Enter exactly three characters. " +
document.form1.threeChar.value + " is not valid.")
return false
}
}
PETLJE
for
for ([initial-expression]; [condition]; [increment-expression]) {
statements
}
Primer:
<SCRIPT>
function howMany(selectObject) {
var numberSelected=0
for (var i=0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected==true)
numberSelected++
}
return numberSelected
}
</SCRIPT>
<FORM NAME="selectForm">
<P><B>Choose some music types, then click the button below:</B>
<BR><SELECT NAME="musicTypes" MULTIPLE>
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
<OPTION> Classical
<OPTION> Opera
</SELECT>
<P><INPUT TYPE="button" VALUE="How many are selected?"
onClick="alert ('Number of options selected: ' +
howMany(document.selectForm.musicTypes))">
</FORM>
while
while (condition) {
statements
}
Primer:
n = 0
x = 0
while( n < 3 ) {
n ++
x += n
}
while (true) {
alert("Hello, world") }
break
function testBreak(x) {
var i = 0
while (i < 6) {
if (i == 3)
break
i++
}
return i*x
}
continue
i = 0
n = 0
while (i < 5) {
i++
if (i == 3)
continue
n += i
}
Switch
switch ( numberthing ) {
case firstval:
statements;
break;
case secondval:
statements;
break;
...
default:
statements;
break;
}
Primer:
function balance(int which) {
String retval=;
switch ( which ) {
case 8:
retval = Good;
break;
case 13:
retval = Not Good;
break;
default:
retval = Error;
break;
}
return(retval);
DATE Objekat
1.
2.
3.
4.
dateObjectName
dateObjectName
dateObjectName
dateObjectName
=
=
=
=
new
new
new
new
Date()
Date("month day, year hours:minutes:seconds")
Date(year, month, day)
Date(year, month, day, hours, minutes, seconds)
Metodi:
getDate
getDay
getHours
getMinutes
getMonth
getSeconds
getTime
getYear
setDate
setHours
setMinutes
setMonth
setSeconds
setTime
setYear
toString
Primeri:
today = new Date()
birthday = new Date("December 17, 1995 03:24:00")
birthday = new Date(95,12,17)
birthday = new Date(95,12,17,3,24,0)
cas=today.getHours()
minuti=today.getMinutes()
sekunde=today.getSeconds()
String Objekat
Property Description
length
Duina stringa
Methodi
big
blink
bold
fontcolor
sup
sub
fontsize
italics
small
strike
indexOf
lastIndexOf
substring
toLowerCase
toUpperCase
charAt
Primer
var last_name = "Schaefer"
last_name.length
last_name.toUpperCase()
last_name.toLowerCase()
last_name.charAt(0)
last_name.indexOf(hae)
last_name.indexOf(e,5)
last_name.lastIndexOf(e)
last_name.lastIndexOf(e,5)
last_name.substring(1,5)
last_name.substring(1,50)
8
"SCHAEFER"
"schaefer"
S
2
6
6
4
chae
chaefer
Obrada dogadjaja
Dogadjaj
Primena na
Kada se pojavljuje
Metod
abort
images
blur
windows, frames, and all form User removes input focus from
elements
window, frame, or form element
onBlur
click
onClick
change
onChange
error
images, windows
focus
windows, frames, and all form User gives input focus to window,
elements
frame, or form element
load
document body
onFocus
mouseover links
onMouseOver
reset
onReset
forms
select
onSelect
submit
submit button
onSubmit
unload
document body
onUnload
Cookies
name=value
[;EXPIRES=dateValue]
[;DOMAIN=domainName]
[;PATH=pathName]
[;SECURE]
var citamCookie=document.cookie
document.cookie = name=+vrednostKojuPamtim+;secure