"use strict";
Definierar det JavaScript-kod ska köras i "strikt läge".
Direktivet "use strict"
var nytt i ECMAScript version 5.
Det är inte ett uttalande, utan ett bokstavligt uttryck, ignorerat av tidigare versioner av JavaScript.
Syftet med "use strict"
är att indikera att koden ska köras i "strict mode".
Med strikt läge kan du till exempel inte använda odeklarerade variabler.
Alla moderna webbläsare stöder "use strict" förutom Internet Explorer 9 och lägre:
Directive | |||||
---|---|---|---|---|---|
"use strict" | 13.0 | 10.0 | 4.0 | 6.0 | 12.1 |
Siffrorna i tabellen anger den första webbläsarversionen som fullt ut stöder direktivet.
Du kan använda strikt läge i alla dina program. Det hjälper dig att skriva renare kod, som att hindra dig från att använda odeklarerade variabler.
"use strict"
är bara en sträng, så IE 9 kommer inte att ge ett fel även om den inte förstår det.
Strikt läge deklareras genom att lägga till "use strict"; i början av en skript eller en funktion.
Deklareras i början av ett skript, det har globalt omfång (all kod i skriptet kommer att köras i strikt läge):
"use strict";
x = 3.14; // This will cause an error
because x is not declared
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14; // This will cause an error (x is not defined).
</script>
</body>
</html>
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will also cause an error
because y is not declared
}
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>Global "use strict" declaration.</h2>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will cause an error (y is not defined)
}
</script>
</body>
</html>
Deklareras inuti en funktion, den har lokalt omfång (endast koden inuti funktionen är i strikt läge):
x = 3.14; // This will not cause an error.
myFunction();
function
myFunction() {
"use strict";
y = 3.14; // This will cause an error
}
Prova själv →
<!DOCTYPE html>
<html>
<body>
<p>"use strict" in a function will only cause errors in that function.</p>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // This will cause an error (y is not defined).
}
</script>
</body>
</html>
Syntaxen, för att deklarera strikt läge, designades för att vara kompatibel med äldre versioner av JavaScript.
Att sammanställa en numerisk bokstavlig (4 + 5;) eller en strängliteral ("John Doe";) i en JavaScript-programmet har inga biverkningar. Den kompileras helt enkelt till en icke existerande variabel och dör.
Så "use strict";
spelar bara roll för nya kompilatorer som "förstår" innebörden av det.
Strikt läge gör det lättare att skriva "säkert" JavaScript.
Strikt läge ändrar tidigare accepterad "dålig syntax" till verkliga fel.
Som ett exempel, i normal JavaScript, skapas en ny om du skriver ett variabelnamn fel global variabel. I strikt läge kommer detta att ge ett fel, vilket gör det omöjligt att av misstag skapa en global variabel.
I normal JavaScript kommer en utvecklare inte att få någon felfeedback tilldela värden till icke-skrivbara egenskaper.
I strikt läge, alla tilldelningar till en icke-skrivbar egenskap, endast en getter egenskap, en icke-existerande egenskap, en icke-existerande variabel eller en icke-existerande objekt, kommer att ge ett fel.
Det är inte tillåtet att använda en variabel utan att deklarera den:
"use strict";
x = 3.14; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14; // This will cause an error (x is not defined).
</script>
</body>
</html>
Objekt är också variabler.
Att använda ett objekt, utan att deklarera det, är inte tillåtet:
"use strict";
x = {p1:10, p2:20}; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using an object without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = {p1:10, p2:20}; // This will cause an error (x is not defined).
</script>
</body>
</html>
Det är inte tillåtet att ta bort en variabel (eller ett objekt).
"use strict";
let x = 3.14;
delete x; // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a variable (or object) is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 3.14;
delete x; // This will cause an error
</script>
</body>
</html>
Det är inte tillåtet att ta bort en funktion.
"use strict";
function x(p1, p2) {};
delete x;
// This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a function is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p2) {};
delete x; // This will cause an error
</script>
</body>
</html>
Det är inte tillåtet att duplicera ett parameternamn:
"use strict";
function x(p1, p1) {}; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Duplicating a parameter name is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p1) {}; // This will cause an error
</script>
</body>
</html>
Oktala numeriska bokstaver är inte tillåtna:
"use strict";
let x = 010; // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal numeric literals are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 010; // This will cause an error
</script>
</body>
</html>
Octal escape-tecken är inte tillåtna:
"use strict";
let x = "\010"; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal escape characters are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = "\010"; // This will cause an error
</script>
</body>
</html>
Det är inte tillåtet att skriva till en skrivskyddad egenskap:
"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a read-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This will cause an error
</script>
</body>
</html>
Det är inte tillåtet att skriva till en semesterbostad:
"use strict";
const obj = {get x()
{return 0} };
obj.x = 3.14; // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a get-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {get x() {return 0} };
obj.x = 3.14; // This will cause an error
</script>
</body>
</html>
Det är inte tillåtet att ta bort en egendom som inte går att radera:
"use strict";
delete Object.prototype; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting an udeletable property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
delete Object.prototype; // This will cause an error
</script>
</body>
</html>
Ordet eval
kan inte användas som en variabel:
"use strict";
let eval = 3.14; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "eval" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let eval = 3.14; // This will cause an error
</script>
</body>
</html>
Ordet argument
kan inte användas som en variabel:
"use strict";
let arguments = 3.14; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "arguments" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let arguments = 3.14; // This will cause an error
</script>
</body>
</html>
with
-satsen är inte tillåten:
"use strict";
with (Math){x = cos(2)}; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The with statement is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
with (Math){x = cos(2)}; // This will cause an error
</script>
</body>
</html>
Av säkerhetsskäl är det inte tillåtet att skapa eval()
variabler i det omfång från vilket det anropades.
I strikt läge kan en variabel inte användas innan den deklareras:
"use strict";
eval ("x = 2");
alert (x); // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
I strikt läge kan eval() inte deklarera en variabel med nyckelordet var:
"use strict";
eval ("var x = 2");
alert (x); // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("var x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
eval() kan inte deklarera en variabel med nyckelordet let:
eval ("let x = 2");
alert (x); // This
will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>Using eval()</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
eval ("let x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
Nyckelordet detta
i funktioner fungerar annorlunda i strikt läge.
Nyckelordet this
refererar till objektet som kallas funktionen.
Om objektet inte är specificerat fungerar det i strikt läge returnerar odefinierad
och fungerar normalt läge returnerar det globala objektet (fönster):
"use strict";
function myFunction() {
alert(this); // will alert "undefined"
}
myFunction();
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Inside functions, the "this" keyword is no longer the global object if not specified:</h3>
<script>
"use strict";
function myFunction() {
alert(this);
}
myFunction();
</script>
</body>
</html>
Nyckelord reserverade för framtida JavaScript-versioner kan INTE användas som variabel namn i strikt läge.
Dessa är:
"use strict";
let public = 1500; // This will cause an error
Prova själv →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Future reserved keywords are not allowed in strict mode.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let public = 1500; // This will cause an error
</script>
</body>
</html>
Direktivet "använd strikt" känns bara igen i början av ett skript eller en funktion.