Zaklady JS

Pole

var pole = new Array("a", "b");
pole.push("c");
var delka = pole.length
 
// length neni pocet prvku pole, ale index posledniho prvku,
 
pole.length = 3; // oreze pole na 3 prvky
 
numbers[numbers.length] = 4; // prida prvek 4 na konec pole
numbers.push(4); // to same jako vyse
 
// numbers is ['zero', 'one', undefined, 'shi', 'go']
numbers.splice(2, 1); // ??? odstran vsechny prvky od indexu "2" a celkem odstran 1 prvek
// numbers is ['zero', 'one', 'shi', 'go']

3D pole

var matrix = [ [0, 1, 2], [3, 4, 5], [6, 7, 8]];
matrix[2][1] // 7
 
for (i = 0; i < n; i += 1) {
   my_array[i] = [];
}

Funkce

Libovolny pocet argumentu

function printOut(para1, para2){
if(arguments.length == 2){
alert("2");
}else if(arguments.length == 1){
alert("1");
}
}

Function literal

var add = function (a, b) { return a + b;};

Objekty - vlastnosti

function checkObject(obj){
if(typeof obj == 'undefined'){
}else if(typeof obj == "string"){
} 
}

String

Test na string

if(typeof obj == "string"){
} 

Rozdelit string na pole

var str = "ab.cd.ce";
var pole = str.split(".");

Promenne - scope

function foo(){
var bar = 1; // lokalni v ramci fce
}
function foo(){
bar = 1; // globalni
}
var bar = 1; // globalni

Arguments

var sum = function ( ) { 
var i, sum = 0; 
for (i = 0; i < arguments.length; i += 1) { 
sum += arguments[i]; 
} return sum; 
};
 
document.writeln(sum(4, 8, 15, 16, 23, 42)); // 108

Try-catch-Throw

function (a, b) {
 if (typeof a !== 'number' || typeof b !== 'number') {
throw { name: 'TypeError', message: 'add needs numbers' };
 } 
return a + b; 
}
 
try {
 add("seven"); 
} catch (e) { 
document.writeln(e.name + ': ' + e.message); 
}

Rozsirujici typy

// zaokrouhleni
Number.method('integer', function ( ) { return Math[this < 0 ? 'ceil' : 'floor'](this); });
document.writeln((10 / 3).integer( )); // vrati 3
 
// uprava Stringu, odebrani mezery na konci
String.method('trim', function ( ) { return this.replace(/^+|+$/g, ''); });

Callbacks

request = prepare_the_request( ); 
send_request_asynchronously(request, function (response)
{ display(response); });

Objects literal

var flight = { 
airline: "Oceanic", 
departure: { 
  IATA: "SYD",
  city: "Sydney" 
  }};
 
  flight.departure.IATA // kdyz je to nedefinovana property, vrati undefined
  var middle = stooge["middle-name"] || "(none)";
  var status = flight.status || "unknown";
 
  var MYAPP = {};
  MYAPP.flight = { airline: "Oceanic",...} // zabaleni cele aplikace do MYAPP pro eliminaci konfliktu jmen)

Moduly

var serial_maker = function ( ) {
 
// Produce an object that produces unique strings. A
// unique string is made up of two parts: a prefix
// and a sequence number. The object comes with
// methods for setting the prefix and sequence
// number, and a gensym method that produces unique
// strings. 
 
var prefix = '';
var seq = 0; 
return { 
   set_prefix: function (p) {
       prefix = String(p); 
       }, 
   set_seq: function (s) {
       seq = s;
       }, 
   gensym: function ( ) { 
       var result = prefix + seq;
       seq += 1;
       return result; 
       } 
      };
     }( );
 
var seqer = serial_maker( );
seqer.set_prefix = 'Q';
seqer.set_seq = 1000;
var unique = seqer.gensym( ); // unique is "Q1000"
 
/disk_1/www/cz/ladariha/wiki/data/pages/programovani/javascript/zaklady.txt · Poslední úprava: 2011/07/29 22:26 autor: ladariha
 
Kromě míst, kde je explicitně uvedeno jinak, je obsah této wiki licencován pod následující licencí:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki