web: js operatory

# Web

Menu

- HTML
- CSS
- JS
  - Úvod
  - Operátory
  - Reg Exp tabulka
  - Příklady1
  - Příklady2
  - PříkladyX
- PHP
- ASP
- MySQL
- Oracle
- Links

## JS Operátory

Logicke funkce

    AND         OR          NOT         XOR
    a 0 0 1 1   a 0 0 1 1               a 0 0 1 1
    b 0 1 0 1   b 0 1 0 1   a 0 1       b 0 1 0 1
    z 0 0 0 1   z 0 1 1 1   z 1 0       z 0 1 1 0

Typ | Op | popis
--- | --- | ---
Arithmeticke | + | a + b scitani
++ | (Increment) +1 promenne
- | a - b odcitani -a negace
-- | (Decrement) -1 promenne
* | a * b nasobeni
/ | a / b deleni
% | (Modulus) zbytek po celociselnem deleni
String | + | a + b spojeni
+= | a+=b -> a = a + b spojeni
Logicke porovnavaci | && | (AND) false && cokoliv = false (zkracena logika, druhy vyraz se uz nevyhodnocuje)
|| | (OR) true || cokoliv = true (zkracena logika, druhy vyraz se uz nevyhodnocuje)
! | (NOT) !a , jestlize "a"=true, vraci false, jinak true
Logicke bitove
& | (AND) a & b = vraci bity, ktere ma "a" i "b" =1
^ | (XOR) a^b = vraci bity, ktere ma "a" a "b" rozdilne
| | (OR) a|b = vrati bity, ktere aspon "a" nebo "b" ma =1
~ | (NOT) ~a = negace bitu 0 za 1, 1 za 0, a=0111 ~a=1000
<< | (Left shift) posun o 1 bit doleva a=111, a<<1 = 1110
>> | (Sign-propagating right shift) posun o 1 bit doprava s pamatovanim znamenka a=1101, a>>1 = 110
>>> | (Zero-fill right shift) ???
prirazeni cisel | =& | normalni prirazeni
+= | 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
porovnani true/false | == | ? a=b
!= | ? a<>b
=== | ? a=b a soucasne stejny typ promennych
!== | ? a<>b a soucasne i ruzny typ promennych
> | ? a>b
>= | ? a>=b
< | ? a<b
<= | ? a<=b
special | ?: | viz podminky, if..then..else
, | vyhodnoceni 2 vyrazu najednou, vraci vysledek posledniho
delete | smaz objekt, property nebo element; upresneno v zavorce indexem pole
function | definovani anonymni funkce
in | true, kdyz se definovana vlastnost nachazi v poli
instanceof | true, kdyz dany objekt je zvoleneho typu
new | vytvor novy objekt zvoleneho typu
this | zkratka odkazujici na aktualni objekt
typeof | vraci retezec typu objektu, vyrazu, pokud nejaky ma
void | definovany vyraz se vykona bez navratu hodnoty

    Vysledky vyrazu:
    cislo - integer, float
    retezec - "hfdh" nebo ""
    1/0 - boolean/cislo hodnota a vysledky podminek
    true/false - boolean hodnota a vysledky podminek
    typ objektu - napr [table Array]
    NaN - hodnota nejde spocitat
    undefined - neni definovany
    Null - definovany, ale prazdny (zadny objekt, cislo, nic)

    porovnani
    vysledek true: a=3 , b=4 (promenne)
    (cisla se konvertuji na spravny typ automaticky)

    3 == a	"3" == a	3 == '3'
    var1 != 4	a != "3"
    3 === a
    var1 !== "3"	3 !== '3'
    var2 > a
    var2 >= a	a >= 3
    var1 < b
    var1 <= b	b <= 5

    1/2 = 0.5
    13%5 = 3 (2 + [3]/5 ; [3] = modulo)
    a=3; a++ (a=3+1=4)
    a=3; b=a++; // b=3;a=4
    a=3; b=++a; // b=4;a=4
    a=3; b=a--; // b=3;a=2
    a=3; b=--a; // b=2;a=2
    a=3; b=-a;  // b=-3;a=3;

    binary
    15 & 9	//  9 (1111 & 1001 = 1001)
    15 | 9	// 15 (1111 | 1001 = 1111)
    15 ^ 9	//  6 (1111 ^ 1001 = 0110)
    9<<2	//  9 (1001 -> 100100)
    9>>2	//  2 (1001 -> 10)
    -9>>2	// -3 (neco kvuli znamenku, neumim ted vysvetlit)
    19>>>2	//  4 (10011 -> 100)
    ... by mohlo ... ale nevim -19>>>2	//  -4 (10011 -> 100)

    logicke
    a1=true  && true	// t && t = true
    a2=true  && false	// t && f = false
    a3=false && true	// f && t = false
    a4=false && (3 == 4)	// f && f = false
    a5="Cat" && "Dog"	// t && t = Dog
    a6=false && "Cat"	// f && t = false
    a7="Cat" && false	// t && f = false

    o1=true  || true	// t || t = true
    o2=false || true	// f || t = true
    o3=true  || false	// t || f = true
    o4=false || (3 == 4)	// f || f = false
    o5="Cat" || "Dog"	// t || t = Cat
    o6=false || "Cat"	// f || t = Cat
    o7="Cat" || false	// t || f = Cat

    n1=!true	// !t = false
    n2=!false	// !f = true
    n3=!"Cat"	// !t = false

    specialni
    z=a?b:c;
    a=true  -> z=b
    a=false -> z=c

    for (var i=0, j=9; i <= 9; i++, j--)
       document.writeln("a["+i+","+j+"]= " + a[i,j])

    delete objectName
    delete objectName.property
    delete objectName[index]
    delete property // legal only within a with statement

    Pr.
    var y = 43
    myobj = new Number()
    myobj.h = 4	// vytvorit vlastnost (property) "h"
    delete x	// = true  (can delete if declared implicitly)
    delete y	// = false (nelze smazat, jestlize je deklarovana s "var")
    delete Math.PI	// = false (nelze smazat predefinovane vlastnosti - property)
    delete myobj.h	// = true  (lze smazat uzivatelem definovane vlastnosti - property)
    delete myobj	// = true  (lze smazat objekt)

    Pr.
    trees=new Array("redwood","bay","cedar","oak","maple")
    delete trees[3]
    if (3 in trees) { /* this does not get executed */ }
    Pr. ALE!
    trees=new Array("redwood","bay","cedar","oak","maple")
    trees[3]=undefined
    if (3 in trees) { /* this gets executed */ }

    function
    promenna jako funkce
    var x = function(y) {return y*y};

    pole funkci
    var a = [function(y) {return y}, function y {return y*y}, function (y) [return y*y*y}];
    a[0](5) = 5
    a[1](5) = 25
    a[2](5) = 125

    in
    // Arrays
    trees=new Array("redwood","bay","cedar","oak","maple")
    0 in trees        // = true
    3 in trees        // = true
    6 in trees        // = false
    "bay" in trees    // = false (you must specify the index number, not the value at that index)
    "length" in trees // = true (length is an Array property)

    // Predefined objects
    "PI" in Math		// = true
    myString=new String("coral")
    "length" in myString	// = true

    // Custom objects
    mycar = {make:"Honda",model:"Accord",year:1998}
    "make" in mycar		// = true
    "model" in mycar	// = true

    color1=new String("green")
    "length" in color1	// = true
    color2="coral"
    "length" in color2	// generates an error (color is not a String object)

    mycar = {make:"Honda",model:"Accord",year:1998}
    delete mycar.make
    "make" in mycar		// = false

    trees=new Array("redwood","bay","cedar","oak","maple")
    delete trees[3]
    3 in trees		// = false

    mycar = {make:"Honda",model:"Accord",year:1998}
    mycar.make=undefined
    "make" in mycar		// = true

    trees=new Array("redwood","bay","cedar","oak","maple")
    trees[3]=undefined
    3 in trees		// = true

    instanceof (parametry objectName, objectType)
    Pr.
    color1=new String("green")
    color1 instanceof String // returns true
    color2="coral"
    color2 instanceof String // returns false (color is not a String object)

    Pr.
    theDay=new Date(1995, 12, 17)
    if (theDay instanceof Date) {
       // statements to execute
    }

    Pr.
    myString=new String()
    myDate=new Date()

    myString instanceof String // returns true
    myString instanceof Object // returns true
    myString instanceof Date   // returns false

    myDate instanceof Date     // returns true
    myDate instanceof Object   // returns true
    myDate instanceof String   // returns false

    Pr.
    function Car(make, model, year) {
       this.make = make
       this.model = model
       this.year = year
    }
    mycar = new Car("Honda", "Accord", 1998)
    a=mycar instanceof Car // returns true
    b=mycar instanceof Object // returns true

    new
    Syntax: objectName = new objectType (param1 [,param2] ...[,paramN])
    Parameters: objectName, objectType, param1...paramN

    (prototype)
    Pr.
    Car.prototype.color=null
    car1.color="black"
    birthday.description="The day you were born"

    Pr.
    function car(make, model, year) {
       this.make = make
       this.model = model
       this.year = year
    }

    Now you can create an object called mycar as follows:

    mycar = new car("Eagle", "Talon TSi", 1993)
    kenscar = new car("Nissan", "300ZX", 1992)

    Pr.
    function person(name, age, sex) {
       this.name = name
       this.age = age
       this.sex = sex
    }

    And then instantiate two new person objects as follows:

    rand = new person("Rand McNally", 33, "M")
    ken = new person("Ken Jones", 39, "M")

    Pr.
    function car(make, model, year, owner) {
       this.make = make;
       this.model = model;
       this.year = year;
       this.owner = owner;
    }

    To instantiate the new objects, you then use the following:

    car1 = new car("Eagle", "Talon TSi", 1993, rand);
    car2 = new car("Nissan", "300ZX", 1992, ken)
    xxx = car2.owner.name;

    this
    Syntax: this[.propertyName]

    Pr.
    function validate(obj, lowval, hival) {
       if ((obj.value < lowval) || (obj.value > hival))
          alert("Invalid Value!")
    }

    <B>Enter a number between 18 and 99:</B>
    <INPUT TYPE = "text" NAME = "age" SIZE = 3
       onChange="validate(this, 18, 99)">

    type of

    Pr.
    var myFun = new Function("5+2")
    var shape="round"
    var size=1
    var today=new Date()

    The typeof operator returns the following results for these variables:

    typeof myFun is object
    typeof shape is string
    typeof size is number
    typeof today is object
    typeof dontExist is undefined

    For the keywords true and null, the typeof operator returns the following results:

    typeof true is boolean
    typeof null is object

    typeof 62 is number
    typeof 'Hello world' is string

    For property values, the typeof operator returns the type of value the property contains:

    typeof document.lastModified is string
    typeof window.length is number
    typeof Math.LN2 is number

    For methods and functions, the typeof operator returns results as follows:

    typeof blur is function
    typeof eval is function
    typeof parseInt is function
    typeof shape.split is function

    For predefined objects, the typeof operator returns results as follows:

    typeof Date is function
    typeof Function is function
    typeof Math is function
    typeof Option is function
    typeof String is function

    void
    1. void (expression)
    2. void expression

    Pr.
    <A HREF="javascript:void(0)">Click here to do nothing</A>

    The following code creates a hypertext link that submits a form when the user clicks it.

    <A HREF="javascript:void(document.form.submit())">
    Click here to submit</A>

    Jestli to spravne chapu, tak funkce se vykona fiktivne.
    V podstate to ma smysl v jedinem pripade, ze chceme znat vysledek nejake akce, a nechceme zmenit nejake udaje.
    Mam pocit, ze kdybych udelal funkci pro opravu hodnot na obrazovce,
    ktera ma jeste nejaky vystup, tak vy se na obrazovce nic nezmenilo,
    ale vystup bych mohl priradit do nejake promenne.
    (jenze to lze udelat i podminkou a parametrem funkce)

2005-2009 Peter Mlích, peter.mlich@volny.cz