Javascript

Object Audio

audio = new Audio(‚sound.mp3‘); audio.play();

Prehrávanie viacerých zvukov

V starom mobile nestaci len zmenit src ale potom treba dat load() a este listener loadeddata az potom play()

sounds = [„sounds/1.mp3“,„sou­nds/2.mp3“,„sou­nds/3.mp3“];

var i = 0;
a.addEventLis­tener(‚ended‘, function() {
this.currentTime = 0;
i++
this.src = sounds[i];

this.load();

}, false);

a.addEventLis­tener(‚loaded­data‘, function() {
alert('loaded **** '+this.src);

this.play();
}, false);

window.setInter­val(„javascript function“, milliseconds);

setInterval(fnc, 1000);

window.clearIn­terval(interval­Variable)

myVar=setInter­val(„javascript function“, milliseconds);
clearInterval(my­Var)

window.setTime­out(„javascript function“, milliseconds);

function

str = „Apple, Banana, Kiwi“
str.slice(0,13);
Apple, Banana

var fruits = [„Banana“, „Orange“, „Apple“, „Mango“];
var energy = fruits.join(" and ");
Banana and Orange and Apple and Mango

vyberie vsetky elementy podla class

$(„.pc“).each(fun­ction(index) {
console.log(this);
});

append, prepand, before, after

vlozi element na koniec za ostatne elementy ktore su v ul
 $("ul").append("<li>xxxxxxxxxx</li>")

vlozi element na zaciatok pred ostatne elementy ktore su v ul
 $("p").prepend("<li>yyy</li>");




Vlozi element za vybraty prvok
 $("img").after("Some text after");

Vlozi element pred vybraty prvok
 $("img").before("Some text before");

alebo javascript:

var btn = document.createElement("BUTTON");        // Create a <button> element
var t = document.createTextNode("CLICK ME");       // Create a text node
btn.appendChild(t);                                // Append the text to <button>
document.body.appendChild(btn);                    // Append <button> to <body>


var x = document.createElement("INPUT");
x.setAttribute("type", "text");


// div is an object reference to a <div> element with class="foo bar"
div.classList.remove("foo");
div.classList.add("anotherclass");

// if visible is set remove it, otherwise add it
div.classList.toggle("visible");

//  add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10 );

alert(div.classList.contains("foo"));

div.classList.add("foo","bar"); //add multiple classes


HTML DOM reference: element.hasChildNodes() Method

HTML DOM reference: element.insertBefore() Method

HTML DOM reference: element.removeChild() Method

HTML DOM reference: element.replaceChild() Method

HTML DOM reference: document.createElement() Method

HTML DOM reference: document.createTextNode() Method

HTML DOM reference: document.adoptNode() Method

HTML DOM reference: document.importNode() Method



          $( "#modal_nahlad_sk" ).empty();
 $( "#v_nahlad" ).clone().appendTo( "#modal_nahlad_sk" );
          $( "#v_nahlad" ).children().clone().appendTo( "#modal_nahlad_sk" );

Prototype

Ak chcem aby sa metody alebo vlastnosti dali menit naraz pre vsetky instancie treba ich dat do prototype. Ak mam definovanu vlastnost napr. this.name v objekte a chcem ju zmenit musim pouzit delete instancia.name a novu name definujem object.prototy­pe.name = ‚jano‘ .

var Person = function(age){
        this.name = 'aaa';
        this.age = age;
        this.female = 'zena';
        this.frustracnaTolerancia

}

Person.prototype = {
  setFrustracnaTolerancia: function(frustracnaTolerancia) {
        this.frustracnaTolerancia = frustracnaTolerancia
  },
  getSubjektivnyPocit: function() {
        return  this.objektivnyTlak / this.frustracnaTolerancia
  }
};


Person.prototype.objektivnyTlak = 100;

var person = new Person();
person.setFrustracnaTolerancia(3)

var person1 = new Person();
person1.setFrustracnaTolerancia(50)

console.log(person.frustracnaTolerancia)
console.log(person1.frustracnaTolerancia)

console.log(person.getSubjektivnyPocit())
console.log(person1.getSubjektivnyPocit())

Person.prototype.name = 'jano';
delete person.name
console.log(person.name) //jano
console.log(person1.name) //aaa

DEDENIE

function Person() {}

Person.prototype.walk = function(){
  alert ('I am walking!');
};
Person.prototype.sayHello = function(){
  alert ('hello');
};

//////////////////////////
function Student() {
  // Call the parent constructor
  Person.call(this);
}

// inherit Person
Student.prototype = Object.create(Person.prototype);

// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;

// replace the sayHello method
Student.prototype.sayHello = function(){
  alert('hi, I am a student');
}

// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
  alert('goodBye');
}

var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();

// check inheritance
alert(student1 instanceof Person); // true
alert(student1 instanceof Student); // true

Css style

$("p").css("background-color", "yellow"); // prida style
$("p").css("background-color", ""); //odstrani style

Cykly

function logArrayElements(element, index, array) {
  console.log('a[' + index + '] = ' + element);

}

// Notice that index 2 is skipped since there is no item at
// that position in the array.
[2, 5, , 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[3] = 9


['a', 'b', 'c', 'd'].forEach(
        function(key, i){
                console.log(key+' --- '+i)
                }
);


var A = {}

A.fnc = function(){
        [2, 5, , 9].forEach(function(){console.log(this);}); //odkazuje na window
        [2, 5, , 9].forEach(function(){console.log(this);}.bind(this)); // odkazuje na objekt

}

A.fnc()

AJAX

        var ajax = new XMLHttpRequest();

        ajax.open('GET','zvuk.mp3', true);
        ajax.responseType = 'arraybuffer';

        ajax.addEventListener("progress", updateProgress, false);
        ajax.addEventListener("load", transferComplete, false);
        ajax.addEventListener("error", transferFailed, false);
        ajax.addEventListener("abort", transferCanceled, false);

ajax.send(null);

Math

Math.floor() zaokruhlene dole
Math.round() zaokruhlene od 0,5 hore
Math.ceil() zaokruhlene hore
Math.random() generuje nahodne cislo medzi 0 a 1. Nikdy nieje 0 ani 1

console.log(Math.floor( Math.random()*2)) // 0 or 1
console.log(Math.floor( Math.random() < 0.5 ? -1 : 1)) // 1 or -1
console.log(Math.floor(Math.random() * 3) - 1) // -1 or 0 or 1