Ondřej Žára, @0ndras
Operátor A % B
vrací zbytek A
po dělení B
Tak určitě.
var a = 17 % 5; // 2
var b = -17 % 5; // -2 (!)
Number.prototype.mod = function(m) {
return (this < 0 ? this % m + m : this % m);
}
var a = (17).mod(5); // 2
var b = (-17).mod(5); // 3
var DATA = [1, 2, 3, -100, 3.14, 555, .69, -123];
// Které číslo je nejmenší?
Math.min.apply(Math, DATA);
// -123
/* Chrome */
Math.min.apply(Math, new Array(125318))
// RangeError: Maximum call stack size exceeded
/* Firefox */
Math.min.apply(Math, new Array(500001))
// RangeError: arguments array passed to Function.prototype.apply is too large
var min = Number.MAX_VALUE; // 1.7976931348623157e+308
for (var i=0; i<DATA.length; i++) {
min = Math.min(min, DATA[i]);
}
var min = 1/0; // Infinity
for (var i=0; i<DATA.length; i++) {
min = Math.min(min, DATA[i]);
}
var min = DATA[0];
for (var i=1; i<DATA.length; i++) {
min = Math.min(min, DATA[i]);
}
var num = parseInt(str, ?);
var A = ["13", "8", "-222"];
var B = A.map(parseInt); // [13, NaN, NaN]
Array.prototype.map = function(cb) {
for (var i=0; i < this.length; i++) {
cb(this[i], i);
/* ... */
}
}
var returnInt = function(x) {
return parseInt(x);
}
["13", "8", "-222"].map(returnInt); // [13, 8, -222]
var praseInt = function(x, y) {
return parseInt(y, x);
}
["13", "8", "-222"].map(praseInt.bind(null, null));
// [13, 8, -222]
["13", "8", "-222"].map(praseInt.bind(null, 16));
// [19, 8, -546]
["3.14", "8", "0x10", ""].map(parseFloat);
// [3.14, 8, 0, NaN]
["3.14", "8", "0x10", ""].map(Number);
// [3.14, 8, 16, 0]
var DICT = {};
DICT["hello"] = "ahoj";
DICT["foo"] = "bar";
function add(key, value) {
DICT[key] = value;
}
add($("#en").value, $("#cs").value);
<input id="en" value="__proto__" />
DICT["__proto__"] = 42;
alert(DICT["__proto__"]); // "[object Object]"
ES5 baby!
var DICT = Object.create(null);
DICT["__proto__"] = 42;
alert(DICT["__proto__"]); // 42
for (var i=0; i<pole.length; i++) {
for (var j=0; j<pole[i].length; j++) {
/* ... */
}
}
for (var i=0; i<pole.length; i++) {
for (var j=0; j<pole[i].length; j++) {
if (pole[i][j] == 0) { break; }
}
}
var ok = true;
for (var i=0; i<pole.length; i++) {
for (var j=0; j<pole[i].length; j++) {
if (pole[i][j] == 0) {
ok = false;
break;
}
}
if (!ok) { break; }
}
dance:
for (var i=0; i<pole.length; i++) {
for (var j=0; j<pole[i].length; j++) {
if (pole[i][j] == 0) { break dance; }
}
}
"problem".fixed(); // "<tt>problem</tt>"
"your mom".big(); // "<big>your mom</big>"
"here".link("http://..."); // "<a href="http://...">here</a>"
anchor, big, blink, bold, fixed, fontcolor, fontsize, italics, link, small, strike, sub, sup
<div>
<a href="#">odkaz</a>
</div>
<div>
<a href="#">odkaz</a>
</div>
var div = document.querySelector("div");
div.insertAdjacentHTML("beforeend", "<a href=...");
insertAdjacentHTML | Tree manipulation |
---|---|
"beforebegin" |
parentNode.insertBefore(..., node) |
"afterbegin" |
insertBefore(..., node.firstChild) |
"beforeend" |
appendChild(...) |
"afterend" |
parentNode.insertBefore(..., node.nextSibling) |