Første
JavaScript "Core Language" serie, Planche nr. 46
Kursus Indhold

Udskrift af : JavaScript "Core Language"

Planche 1 : JavaScript "Core Language"

Omfatter

Planche 2 : JavaScript værdier

Datatyper

Planche 3 : JavaScript værdier 2

Dynamisk definition Kan omdefineres uden problemmer: Automatisk konvertering til tekststrenge med + Mens

Planche 4 : JavaScript Variable

Symbolske navne for værdier: Variable erklæres eksplicit eller implicit

Planche 5 : Variable evaluering

Bruges en variabel uden at være erklæret får den værdien 'undefined'

Det giver runtime-fejl, hvis variablen var erklæret uden var

Ellers returneres 'undefined' eller 'NaN' (numerisk)

Boolsk "værdi" af 'undefined' er 'false'

Planche 6 : Dækningsområde

Eklæringens "Scope" Uden for funktioner - hele dokumentet (vinduet) Inde i funktioner - i denne Globale variable i andre vinduer (frames) Hvor 'parent' peger på en overliggende FRAMESET

Planche 7 : Konstanter

Kan være

Planche 8 : Array Literals

kaffetyper = ["Blå Cirkel", "Rød Cirkel", "Frellsens"]

Array'et fortolkes hver gang det evalueres

Array konstanter er også Array Objekter (se senere)

Tomme kommaer medregnes - undtagen til sidst.

Planche 9 : Andre konstanter

Boolean : Decimaltal : heltal "." heltal " E" heltal Heltal :

Planche 10 : Objekt-konstanter

Består af en liste af "property" navne og værdier i {}

eksempel:

cykel = { minCykel : "Ralaigh", hentCykel : cykelTyper ("Hvis?"), special: farve }

cykel.minCykel giver "Ralaigh" o.s.v.

Planche 11 : String-konstanter

Består at tekster i matchene "" eller ''. Specialtegn angives med \ foran:
Character Meaning
\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Tab

\'

Apostrophe or single quote

\"

Double quote

\\

Backslash character (\)

\XXX

The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.

\xXX

The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.

\uXXXX

The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. See "Unicode Escape Sequences" on page 43.

Planche 12 : Udtryk

JavaScript har 3 typer udtryk

Planche 13 : Operatorer

Operatorer kan være binary eller unary

Planche 14 : Assignment

Assignment operator er =

Andre, forkortede udgaver:
Shorthand operator Meaning
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

Planche 15 : Sammenligninger

Comparison Operators
Operator Description Examples returning true1

Equal (==)

Returns true if the operands are equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.

3 == var1
"3" == var1
3 == '3'

Not equal (!=)

Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.

var1 != 4
var2 != "3"

Strict equal (===)

Returns true if the operands are equal and of the same type.

3 === var1

Strict not equal (!==)

Returns true if the operands are not equal and/or not of the same type.

var1
!== "3"
3 !== '3'

Greater than (>)

Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=)

Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1
var1 >= 3

Less than (<)

Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal (<=)

Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

Planche 16 : Aritmetiske operatorer

Operator Description Example

%
(Modulus)

Binary operator. Returns the integer remainder of dividing the two operands.

12 % 5 returns 2.

++
(Increment)

Unary operator. Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one.

If x is 3, then ++x sets x to 4 and returns 4, whereas x++ sets x to 4 and returns 3.

--
(Decrement)

Unary operator. Subtracts one to its operand. The return value is analogous to that for the increment operator.

If x is 3, then --x sets x to 2 and returns 2, whereas x++ sets x to 2 and returns 3.

-
(Unary negation)

Unary operator. Returns the negation of its operand.

If x is 3, then -x returns -3.

Planche 17 : Bitoperationer

Bitrvis Operatorer
Operator Usage Description

Bitwise AND

a & b

Returns a one in each bit position for which the corresponding bits of both operands are ones.

Bitwise OR

a | b

Returns a one in each bit position for which the corresponding bits of either or both operands are ones.

Bitwise XOR

a ^ b

Returns a one in each bit position for which the corresponding bits of either but not both operands are ones.

Bitwise NOT

~ a

Inverts the bits of its operand.

Left shift

a << b

Shifts a in binary representation b bits to left, shifting in zeros from the right.

Sign-propagating right shift

a >>
b

Shifts a in binary representation b bits to right, discarding bits shifted off.

Zero-fill right shift

a >>> b

Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

Planche 18 : Bit-flytninger

"Bitwise Shift operators"
Operator Description Example

<<
(Left shift)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

9<<2 yields 36, because 1001 shifted 2 bits to the left becomes 100100, which is 36.

>>
(Sign-propagating right shift)

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.

9>>2 yields 2, because 1001 shifted 2 bits to the right becomes 10, which is 2. Likewise, -9>>2 yields -3, because the sign is preserved.

>>>
(Zero-fill right shift)

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.

19>>>2 yields 4, because 10011 shifted 2 bits to the right becomes 100, which is 4. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result.

Planche 19 : Logiske operatorer

Arbejder på boolske ("Boolean") udtryk:
Operator Usage Description

&&

expr1 && expr2

(Logical AND) Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

||

expr1 ||
expr2

(Logical OR) Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

!

!expr

(Logical NOT) Returns false if its single operand can be converted to true; otherwise, returns true.

Planche 20 : Tekst-operatorer

Hvis der indgår String-udtryk, kan '+' og '+=' bruges til at sammenstille tekststrenge med.

Planche 21 : Special operatorer

Betingelser:

Hvis 'betingelse' er 'true' returneres 'værdi1', eller 'værdi2'

Komma-operator:

Begge udtryk udregnes, men kun det sidste returneres.

delete-operator:

Sletter et objekt, en property eller et element i et array.

new-operator : Opretter et nyt objekt af en brugerdefineret eller prædefineret type. this-operator : Betegnelse for aktuelt objekt. typeof-operator: Returnere operandens type i en tekststreng Returnerer "string", hvis vaerdi er tekstvariabel. void-operator: Det efterfølgende udtryks værdi ignoreres Returnerer 'undefined' istedet for tallet 4

Planche 22 : Operator prioritet

Operator precedence
Operator type Individual operators

comma

,

assignment

= += -= *= /= %= <<= >>= >>>= &=
^= |=

conditional

?:

logical-or

||

logical-and

&&

bitwise-or

|

bitwise-xor

^

bitwise-and

&

equality

==
!=

relational

< <= > >=

bitwise shift

<< >> >>>

addition/subtraction

+ -

multiply/divide

* / %

negation/increment

! ~ - + ++ -- typeof void delete

call

()

create instance

new

member

.
[]

Planche 23 : Regulære udtryk

Behandles ikke her.

se kapitel 4 i JavaScript Guide

Planche 24 : Sætninger

Kan være:

Sætninger adskilles af ";"

Alle udtryk er også sætninger.

Planche 25 : Betingelsessætninger

if - else
if ( betingelsesudtryk ) {
    sætninger
}


if ( betingelsesudtryk ) {
    sætninger
}
else {
    sætninger
}

Planche 26 : If-eksempel

I følgende eksempel returnerer checkData værdien 'true', hvis der er præcis 3 tegn i 'Text'-objektet. Ellers vises en advarsel, og værdien 'false' returneres.
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
   }
}

Planche 27 : Switch

switch-udtryk
switch ( udtryk ) {
  case etikette1 :
       sætninger
       break;
  case etikette2 :
       sætninger
       break;
  ...
  default : sætninger
}

Planche 28 : Switch-eksempel

I det følgende eksempel skrives 'Bananas are $0.48 a pound.', hvis expr har værdien "Bananas".
switch (expr) {
   case "Oranges" : 
      document.write("Oranges are $0.59 a pound."); 
      break; 
   case "Apples" :
      document.write("Apples are $0.32 a pound.");
      break;
   case "Bananas" : 
      document.write("Bananas are $0.48 a pound."); 
      break; 
   case "Cherries" :
      document.write("Cherries are $3.00 a pound.");
      break; 
   default :
      document.write("Sorry, we are out of " + i + "."); 
}
document.write("Is there anything else you'd like?");

Planche 29 : Løkke-udtryk


for ([initialExpression]; [condition]; [incrementExpression]) {
   statements
}

do {
   statement
} while (condition)

while (condition) {
   statements
}

Eksempler på While-løkker:
Udføres mindst 1 gang, og indtil i er 5.

do {
   i+=1;
   document.write(i);
} while (i<5);

Udføres 3 gange, og x tælles op med værdien af n hver gang.

n = 0
x = 0
while( n < 3 ) {
   n ++
   x += n
}

Uendelig løkke - afsluttes aldrig:

while (true) {
   alert("Hello, world") }

Planche 30 : Ingen Goto, men...

Goto findes ikke - men der er noget der ligner:

Etikette - "label" - sætning

Break-sætning Continue-sætning Afbryder et loop, og fortsætter med næste værdi.

Planche 31 : Objekt-manipuleringer

for...in sætninger

en slags løkke, der arbejder på objekt-properties:


for (variable in object) {
   statements }

eksempel:

function dump_props(obj, obj_name) {
   var result = ""
   for (var i in obj) {
      result += obj_name + "." + i + " = " + obj[i] + "\n"
   }
   result += "\n-----------\n"
   return result
}

Udskriver alle properties (attributter) for et objekt obj og dets navn obj_name.

Planche 32 : With-sætning

"with" sætter standard (default eller underforstået) objekt for alle sætninger imellem "{" og "}"

with (object){
   statements
}

Planche 33 : Kommentarer

Kommentarer kan skrives overalt i koden, hvis følgende regler overholdes: