Die ECMAScript -Sprache (JavaScript) verfügt über eingebaute Funktionen, die direkt verwendet werden können. Hier werden einige ausgewählte "Built-in Functions" dargestellt.
Technologien ändern sich, sowohl das Machbare, wie das Verfügbare. Die "gängigen" ( proprietären ) Browser erfüllen (2016) ECMAScript 5/6 Standard mit gewissen Einschränkungen, siehe z.B. github ECMAScript compatibility table .
Ein Brwoser hat eingebaute Funktionen, die mit den ECMASxript zusammenwirken
und z.B. für Zugriffe auf DOM-Strukturen benötigt werden
( auch AJAX, atob, btoa, usw. ).
Solche Funktionen sind i.a. an das ddie Objekte window, iframe, frame,
links, location, event, history, usw. gebunden.
Siehe z.B.
Browser-Objekt-Hierarchie
.
ECMAScript (JavaScript) hat nützliche Zeichenketten-Funktionen. In den ("gängigen") Browsern wird unterstützt:
| ECMAScript String Object Reference | |
|---|---|
| Method | Description |
| anchor() | Creates an HTML anchor |
| big() | Displays a string in a big font |
| bold() | Displays a string in bold |
| charAt() | Returns the character at a specified position |
| charCodeAt() | Returns the Unicode of the character at a specified position |
| concat() | Joins two or more strings |
| fixed() | Displays a string as teletype text |
| fontcolor() | Displays a string in a specified color |
| fontsize() | Displays a string in a specified size |
| fromCharCode() | Takes the specified Unicode values and returns a string |
| indexOf() | Returns the position of the first occurrence of a specified string value in a string |
| italics() | Displays a string in italic |
| lastIndexOf() | Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string |
| link() | Displays a string as a hyperlink |
| match() | Searches for a specified value in a string |
| replace() | Replaces some characters with some other characters in a string |
| search() | Searches a string for a specified value |
| slice() | Extracts a part of a string and returns the extracted part in a new string |
| small() | Displays a string in a small font |
| split() | Splits a string into an array of strings |
| strike() | Displays a string with a strikethrough |
| sub() | Displays a string as subscript |
| substr() | Extracts a specified number of characters in a string, from a start index |
| substring() | Extracts the characters in a string between two specified indices |
| sup() | Displays a string as superscript |
| toLowerCase() | Displays a string in lowercase letters |
| toUpperCase() | Displays a string in uppercase letters |
| toSource() | Represents the source code of an object |
| valueOf() | Returns the primitive value of a String object |
| String Object Properties | |
|---|---|
| Property | Description |
| constructor | A reference to the function that created the object |
| length | Returns the number of characters in a string |
| prototype | Allows you to add properties and methods to the object |
In ECMAScript gibt es (ähnlich zur Java-Math-Bibliothek) statischen Methoden, die mit dem Math-Objekt verfügbar sind und z.B. für wissenschaftliche Berechnungen verwendet werden können.
| Funktion | Bedeutung | Funktion | Bedeutung |
|---|---|---|---|
| Math.E | Eulersche Konstante e | Math.ceil() | Liefert für Zwischenwerte die nächsthöhere Ganzzahl |
| Math.PI | für Kreisberechnungen | Math.floor() | Liefert für Zwischenwerte die nächstniedrigere Ganzzahl |
| Math.round() | kaufmännisches Runden | Math.min() | Min-Wert |
| Math.max() | Max-Wert | Math.random() | Pseudo-Zufallszahl zwischen 0.0<= ... 1.0
Beisp.: Math.round(Math.random() * 2147483647) |
| Math.sqrt() | Quadratwurzel | Math.pow(a,b) | liefert ab |
| Math.log() | natürlicher Logarithmus | Math.exp() | e-Funktion |
| Math.cos() | Trigonometrische Funktion | Math.acos() | Trigonometrische Funktion |
| Math.sin() | Trigonometrische Funktion | Math.asin() | Trigonometrische Funktion |
| Math.tan() | Trigonometrische Funktion | Math.atan() | Trigonometrische Funktion |
Reguläre Ausdrücken können dynamisch (zur Laufzeit) mit new RegExp(,) erzeugt werden.
| Konstanten Funktion | Math-Konstanten Math.-Funktionen | Anzeige |
|---|---|---|
|
.LN2 .LN10 .LOG2E .LOG10E .PI .SQRT1_2 .SQRT2 .min() .max() .floor() .ceil() .round() .cos() .sin() .tan() .acos() .asin() .atan() .sqrt() .pow() .exp() .random() |
var s = "E = " + Math.E +"<br />Math.LN2 = " + Math.LN2 +"<br />Math.LN10 = " + Math.LN10 +"<br />Math.LOG2E = " + Math.LOG2E +"<br />Math.LOG10E = " + Math.LOG10E +"<br />Math.PI = " + Math.PI +"<br />Math.SQRT1_2 = " + Math.SQRT1_2 +"<br />Math.SQRT2 = " + Math.SQRT2 +"<br />Math.min(5,-5) = " + Math.min(5,-5) +"<br />Math.max(5,-5) = " + Math.max(5,-5) +"<br />Math.abs(-2.3) = " + Math.abs(-2.3) +"<br />Math.floor( 3.4)= " + Math.floor( 3.4) +"<br />Math.floor(-3.4)= " + Math.floor(-3.4) +"<br />Math.ceil( 3.4) = " + Math.ceil( 3.4) +"<br />Math.ceil(-3.4) = " + Math.ceil(-3.4) +"<br />Math.round( 3.4)= " + Math.round( 3.4) +"<br />Math.round(-3.4)= " + Math.round(-3.4) +"<br />Math.cos(0) = " + Math.cos(0) +"<br />Math.sin(0) = " + Math.sin(0) +"<br />Math.tan(0) = " + Math.tan(0) +"<br />Math.acos(1.2) = " + Math.acos(1.2) +"<br />Math.asin(1) = " + Math.asin(1) +"<br />Math.atan(1) = " + Math.atan(1) +"<br />Math.sqrt(7.02) = " + Math.sqrt(7.02) +"<br />Math.pow(2.01,3)= " + Math.pow(2.01,3) +"<br />Math.exp(1) = " + Math.exp(1) +"<br />Math.log(1) = " + Math.log(1) +"<br />Math.random() = " + Math.random() +"<br />Math.random() = " + Math.random(); document.write(s); |
Ausgabe von s: |
| Funktion | Beispiel | Anzeige |
|---|---|---|
| eval() interpretiert einen String als JavaScript-Programm und führt dieses im aktuellen Sichtbarkeitsbereich aus. |
var a = 3, b = 5, c;
var x = eval("a - 2");
var y = eval("( 21 % 11 ) / b");
var s = "if(c==undefined)z=3;else z=4;"
var z = eval(s);
s = "x="+x + "<br>y="+y + "<br>z="+z;
document.write(s);
Hinweis: | |
| parseFloat() wandelt eine Zeichenfolge in eine Fließkommazahl |
var arr = ["2.5","3,5","abc","1a"];
var str ="";
for(var i=0; i < arr.length; ++i) {
str += "arr["+i+"]=";
str += parseFloat(arr[i])+"<br>";
} document.write(str);
|
NaN-Prüfung: if(isNaN(var)==true) ... |
| parseInt() wandelt eine Zeichenfolge in eine ganze Zahl |
var arr = ["2.5","03,5","abc","1a"];
var a, str ="";
for(var i=0; i < arr.length; ++i) {
e = arr[i];
while (e.charAt(0) == '0')
e = e.substring(1,e.length);
str += "arr["+i+"]=" + +parseInt(e) + "<br>";
} document.write(str);
| |
| parseInt() wandelt eine Zeichenfolge in eine ganze Zahl (radix 0 kann bei einzelnen Browsern zu Fehlern führen) |
function teste_parseInt(radix) {
var arr = ["12.5","013,5","1f","abc"];
var str ="";
for(var i=0; i < arr.length; ++i) {
var e = arr[i];
while (e.charAt(0) == '0')
e = e.substring(1,e.length);
str += "arr["+i+"]=" + parseInt(e,radix) + "<br />";
} return str;
}
var s = "radix 10:<br />"+teste_parseInt(10);
s += "radix 16:<br />"+teste_parseInt(16);
document.write(s);
| |
| btoa(), atob 8-Bit-String und Base64 |
//nur 8-Bit-Zeichen
var my_str = "Testä",
my_64 = window.btoa(my_str),
my_org = window.atob(my_64);
| |
| Number() wandelt String in Zahl |
var date_time = new Date().getTime();
var msec1 = new Date ("Jan 1, 2004 12:00:00");
var msec2 = new Date ("Jan 2, 2004 12:00:00");
var msec = Number(msec2) - Number(msec1);
var str = date_time
+ "<br />Tag=" + msec/1000/60/60+" Stunden";
document.write(str);
| |
| escape() Zeichenfolge |
var s = escape("Hallo,\n Welt");
document.write(s);
| |
| unescape() inverse Funktion zu escape() |
var s = unescape("%41%42%43");
document.write(s);
| |
unescape()-Beispiel:
|
var s="<pre>";
for (var i=0; i<256; i++) {
if((i%8)==0) s += "<br />";
s += "["+i+"]="
s += unescape("%"+i.toString(16));
s += ", "
} s += "</pre>"
document.write(s)
| siehe Ausgabe unten |
Einige Beispiele zu ECMAScript (JavaScript) Zeichenketten-Funktionen.
| Funktion | String-Beispiele | Anzeige |
|---|---|---|
| Kommentare Comments | // Kommentar bis Zeilenende /* MultiLine - Kommentar */ | Viele elementare Sprachkonstrukte sind "recht ähnlich" zur C++- und Java-Syntax |
| ECMAScript- Anweisungen parseInt() substring() typeof() |
<script type="text/javascript">/*<![CDATA[*/
// 012345678901234
var aStr1 = "43_Dies ist der";
var aStr2 = " erste Satz.";
var aNum1 = parseInt(aStr1) - 42;
var aSatz = aNum1;
aSatz += aStr1.substring(2,aStr1.length);
aSatz += aStr2;
document.write(aSatz + "<br />");
document.write("typeof(aNum1)="
+ typeof(aNum1)+"<br />");
document.write("typeof(aStr1)="
+ typeof(aStr1));
/*]]>*/</script>
|
Anzeige: |
| undefined null typeof() |
var a=1, b=new Array(2), c=null, d;
function wrt(s,v) {
var str = "<br>";
//if (v != undefined) { ... }
str +="typeof("+s+")=";
str += typeof(v);
document.write(str);
}
wrt("a",a); wrt("b[1]",b[1]);
wrt("c",c); wrt("d",d);
| Anzeige: |
| toLowerCase() toUpperCase() |
var str = "Hans Weber";
var s1 = str.toLowerCase();
var s2 = str.toUpperCase();
var s3 = s2.substring(1,s2.length-1);
var s = "Lower=" + s1 + "<br>";
s += "Upper=" + s2 + "<br>";
s += "Teil =" + s3;
document.write(s);
|
Ausgabe von s: |
| charCodeAt() fromCharCode() |
Hinweis für mehrere Bytes:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/fromCharCode
var str = "ABC"; var c2 = str.charCodeAt(2); var s = String.fromCharCode(0x41,0x42,c2); document.write(s); |
Ausgabe von s: |
| charAt() |
var str = "ABC"; var c0 = str.charAt(0); var c1 = str.charAt(1); var c2 = str.charAt(2); var s = c0 + c1 + c2; document.write(s); |
Ausgabe von s: |
| str.substr (von,anz) |
var str = "0123456789"; var s6_ = str.substr(6); var s67 = str.substr(6,2); var s = "s6_=" + s6_; s += "<br />s67=" + s67; document.write(s); | |
| str.substring (von,bis) |
var str = "0123456"; var s2_ = str.substring(2); var s23 = str.substring(2,4); var len = str.length; var s56 = str.substring(len-2,len); var s = "s2_=" + s2_; s += "<br />s23=" + s23; s += "<br />s56=" + s56; document.write(s); |
Ausgabe von s: |
| str.split(trenn) |
var str = "0, '1',2,3,";
var arr = str.split(",");
var s="";
for(var i=0;i<arr.length;i++) {
s += "<br />arr["+i+"]=" + arr[i];
s += " mit typeof="+typeof(arr[i]);
}
document.write(s);
| Ausgabe: |
|
.toString() .toFixed()) .toExponential() .toPrecision() .toLocaleString() |
var s, x = 1234.56789; s ="x.toString()="+x.toString()+"<br>" s +="x.toFixed(3)="+x.toFixed(3)+"<br>" s +="x.toExponential(3)="+x.toExponential(3) s +="x.toPrecision(3)="+x.toPrecision(3) s +="x.toLocaleString()="+x.toLocaleString() document.write(s); |
Ausgabe: |
Reguläre Ausdrücken können dynamisch (zur Laufzeit) mit new RegExp(,) erzeugt werden.
| Funktion | String-Beispiele | Anzeige |
|---|---|---|
| .indexOf() .lastIndexOf() .search() |
"Hallo Welt".indexOf('Hallo') liefert 0, "Hallo Welt".indexOf('W') liefert 6, ("Hallo Welt".indexOf(' ') > -1) liefert true.
var str = "xyz3456789xyz345";
var find = "xyz";
var idx1 = str.indexOf(find);
var idx2 = str.indexOf(find,idx1+1);
var idx3 = str.lastIndexOf(find);
var re = new RegExp(find,"gi");
var idx4 = str.search(re);
var s = find+" ab idx="+idx1+"<br>";
s += find+" ab idx="+idx2+"<br>";
s += "last idx3="+idx3+"<br>";
s += "search idx4="+idx4+"<br>";
document.write(s);
|
Ausgabe von s: |
| replace() |
var str = "xyz3456789xyz345";
var s1 = str.replace(/xyz/,"012");
var s2 = str.replace(/xyz/g,"012");
var re = new RegExp("xyz","gi");
var s3 = str.replace(re,"012");
document.write(s1+"<br>"+s2+"<br>"+s3);
|
Ausgabe von s1, s2, s3: |
| .match() |
var str = "xyz3456789xyz345";
var arr = str.match(/xyz/g);
var s = "gefunden=";
s += arr.length + " mal<br>";
s += "arr=" + arr;
document.write(s);
|
Ausgabe von s: |
| .split() .join() .slice() |
var str = "Carl G Jung (Psych)", sep =" ";
var arr = str.split(sep);
var s = arr.slice(0,-1).join("|")+"<br />";
for (var i=0; i < arr.length; ++i) {
s += "arr["+i+"]="+arr[i]+"<br />";
}
document.write(s);
|
Teilstrings von str: |
| re.exec() arr.index arr.lastIndex |
var arr, s = "", str = "Carl G Jung";
var re = new RegExp("\\w+","g");
while( arr = re.exec(str) ) {
s += arr.index; //+"-"+arr.lastIndex;
s += "-"+(arr.index+arr.toString().length);
s += ":"+ arr + "<br>";
}
document.write(s);
|
Durchläuft str, ermittelt auch die Positionen: |
| re.exec() RegExp.$ .split() |
var s1="", s2="<br />", s3="<br />";
var str = "Carl G. Jung";
var re = new RegExp(/(\w.+)\s(\w.+)/g);
var arr = re.exec(str);
for (i=0; i < arr.length; ++i) {
s1 += "arr["+i+"]="+arr[i]+"<br />";
}
for (i=1; i < arr.length; ++i) {
s2 += "RegExp.$"+i+"=";
s2 += eval("RegExp.$"+i)+"<br />";
}
var arr3 = str.split(" ");
for (i=0; i < arr3.length; ++i) {
s3 += "arr3["+i+"]="+arr3[i]+"<br />";
}
document.write(s1+s2+s3);
| einzelne Worte: |
Die eval()-Funktion kann ECMAScript -Source interpretieren und ausführen. Hier ein Test:
Hier der vereinfachte Pseudo-Code:
<form action="" onsubmit="eval(this.TA.value); return false;">
<textarea rows="8" cols="80" name="TA">
var s = "";
for (i=0; i < 10; i++) {
s += i*i + "\n";
} alert("summe s = " + s);
</textarea>
<input type="submit" value="evaluate code" />
</form>