JavaScript: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(→Arrays) |
|||
Zeile 30: | Zeile 30: | ||
<pre>var hypotenuse = function(a,b) { return Math.sqrt(a*a+b*b); } | <pre>var hypotenuse = function(a,b) { return Math.sqrt(a*a+b*b); } | ||
console.log(hypotenuse(1, 44)); | console.log(hypotenuse(1, 44)); | ||
var addAll = function(){ | |||
var rc=0; | |||
for (var ix=0; ix < arguments.length; ix++) | |||
rc += arguments[ix]; | |||
return rc; | |||
} | |||
addAll(1, 2, 3) === 6; | |||
</pre> | </pre> | ||
Version vom 23. Mai 2018, 11:38 Uhr
DOM-Elemente finden
var list = document.getElementsByTagName("UL")[0]; var elem = document.getElementById("xyz");
Einbindung in HTML
<button onclick="myFunction()">Click me</button>
<p onclick="myFunction()">Click me to change my text color.</p> <script> function myFunction() { document.getElementById("demo").style.color = "red"; }
<button onclick="myFunction()">Copy Text</button> <script> function myFunction() { document.getElementById("text2").value = document.getElementById("text2").value; document.getElementById("textarea2").innerHTML = document.getElementById("textarea2").innerHTML; var field = document.getElementById("field1"); field.style.visibility = field.style.visibility == "hidden" ? "visible" : "hidden"; } </script>
Methoden
var hypotenuse = function(a,b) { return Math.sqrt(a*a+b*b); } console.log(hypotenuse(1, 44)); var addAll = function(){ var rc=0; for (var ix=0; ix < arguments.length; ix++) rc += arguments[ix]; return rc; } addAll(1, 2, 3) === 6;
Strikte Gleichheit
- a == b: Referenzvergleich
- a === b: Inhaltsvergleich
Typen
String
text += "."; "xyz".substr(1, 2) === "y"; "abc".indexOf("bc") === 1; "abcb".replace("b", "x") === "axcb";
Numbers
- Nur Gleitpunktzahlen werden benutzt.
- Daher haben Ganzzahlen max. 53 Bit
- x=Math.min(1,2,3); r=Math.floor(x); w=Math.sqrt(x);
Dictionary
var x = { "zahl" : 33, "pair" : { "x" : true } };
Arrays
a=[1, 2, "wow"]; a0=a.shift(); a === [2, "wow"]; a.push(99); a === [2, "wow", 99]; a.concat([2, 3]) === [2, "wow", 99, 2, 3]; [1, 3, 5, 9].slice(0,2) === [1, 3] && [1, 3, 5, 9].slice(2,3) === [3]; [3, 9, 12].join(" ") === "3 9 12";
Klassen
'use strict'; class Polygon { constructor(height, width) { this.name = 'Polygon'; this.height = height; this.width = width; } sayName() { ChromeSamples.log('Hi, I am a ', this.name + '.'); } sayHistory() { ChromeSamples.log('"Polygon" is derived from the Greek polus (many) ' + 'and gonia (angle).'); } static className() { return "Polygon"; } } const p = new Polygon(300, 400); p.sayName();