Gamle JS-versioner er navngivet med numre: ES5 (2009) og ES6 (2015).
Fra 2016 er versionerne navngivet efter år: ECMAScript 2016, 2017, 2018, 2019, ...
Dette kapitel introducerer de nye funktioner i ECMAScript 2017:
JavaScript streng polstring
JavaScript-objektposter()
JavaScript-objektværdier()
JavaScript asynkron og afvent
JavaScript Object.getOwnPropertyDescriptors
ECMAScript 2017 føjede to strengmetoder til JavaScript: padStart()
og padEnd()
for at understøtte udfyldning i begyndelsen og slutningen af en streng.
let text = "5";
text = text.padStart(4,0);
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padStart() Method</h2>
<p>The padStart() method pads a string from the start.</p>
<p>It pads the string with another string (multiple times) until it reaches a given length.</p>
<p id="demo"></p>
<script>
let text = "5";
text = text.padStart(4,"0");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
let text = "5";
text = text.padEnd(4,0);
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The padEnd() Method</h2>
<p>The padEnd() method pads a string at the end.</p>
<p>It pads the string with another string (multiple times) until it reaches a given length.</p>
<p id="demo"></p>
<script>
let text = "5";
text = text.padEnd(4,"0");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
JavaScript-strengpolstring er understøttet i alle moderne browsere siden april 2017:
Chrome 57 | Edge 15 | Firefox 48 | Safari 10 | Opera 44 |
Mar 2017 | Apr 2017 | Aug 2016 | Sep 2016 | Mar 2017 |
ECMAScript 2017 føjede metoden Object.entries()
til objekter.
Object.entries()
returnerer en matrix af nøgle/værdi-parrene i et objekt:
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
let text = Object.entries(person);
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>The Object.entries() method returns an array of the key/value pairs in an object:</p>
<p id="demo"></p>
<script>
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
let text = Object.entries(person);
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Object.entries()
gør det nemt at bruge objekter i sløjfer:
const fruits = {Bananas:300, Oranges:200, Apples:500};
let text = "";
for (let [fruit, value] of Object.entries(fruits)) {
text += fruit + ": " + value + "<br>";
}
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>Object.entries() makes it simple to use objects in loops:</p>
<p id="demo"></p>
<script>
const fruits = {Bananas:300, Oranges:200, Apples:500};
let text = "";
for (let [fruit, amount] of Object.entries(fruits)) {
text += fruit + ": " + amount + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Object.entries()
gør det også nemt at konvertere objekter til kort:
const fruits = {Bananas:300, Oranges:200, Apples:500};
const myMap = new Map(Object.entries(fruits));
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>Object.entries() makes it simple to convert Object to Map:</p>
<p id="demo"></p>
<script>
const fruits = {Bananas:300, Oranges:200, Apples:500};
const myMap = new Map(Object.entries(fruits));
document.getElementById("demo").innerHTML = myMap;
</script>
</body>
</html>
Object.entries()
er understøttet i alle moderne browsere siden marts 2017:
Chrome 47 | Edge 14 | Firefox 47 | Safari 10.1 | Opera 41 |
Jun 2016 | Aug 2016 | Jun 2016 | Mar 2017 | Oct 2016 |
Object.values()
ligner Object.entries()
, men returnerer en enkelt dimensionsmatrix af objektværdierne:
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
let text = Object.values(person);
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Methods</h2>
<p>The Object.values() method returns an array of values from an object:</p>
<p id="demo"></p>
<script>
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
let text = Object.values(person)
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Object.values()
er understøttet i alle moderne browsere siden marts 2017:
Chrome 54 | Edge 14 | Firefox 47 | Safari 10.1 | Opera 41 |
Oct 2016 | Aug 2016 | Jun 2016 | Mar 2017 | Oct 2016 |
async function myDisplay() {
let myPromise = new Promise(function(myResolve, myReject) {
setTimeout(function() { myResolve("I love You !!"); }, 3000);
});
document.getElementById("demo").innerHTML = await myPromise;
}
myDisplay();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript async / await</h2>
<p>Wait 3 seconds (3000 milliseconds) for this page to change.</p>
<h1 id="demo"></h1>
<script>
async function myDisplay() {
let myPromise = new Promise(function(resolve) {
setTimeout(function() {resolve("I love You !!");}, 3000);
});
document.getElementById("demo").innerHTML = await myPromise;
}
myDisplay();
</script>
</body>
</html>
Firefox og Chrome var de første browsere med understøttelse af asynkrone JavaScript-funktioner:
Chrome 55 | Edge 15 | Firefox 52 | Safari 11 | Opera 42 |
Dec 2016 | Apr 2017 | Mar 2017 | Sep 2017 | Dec 2016 |