java-script:strict-mode
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-script:strict-mode [2023/08/09 12:33] – odefta | java-script:strict-mode [2023/08/09 12:41] (current) – odefta | ||
---|---|---|---|
Line 41: | Line 41: | ||
===== Examples ===== | ===== Examples ===== | ||
+ | ==== This will not fallback to window in strict mode ==== | ||
+ | |||
+ | <code javascript> | ||
+ | "use strict"; | ||
+ | |||
+ | function sayHello() { | ||
+ | console.log(this.name, | ||
+ | } | ||
+ | |||
+ | // ... rest of the code ... | ||
+ | |||
+ | sayHello(); // TypeError: Cannot read properties of undefined (reading ' | ||
+ | </ | ||
+ | In strict mode, **this** inside the **sayHello** function will be **undefined** when called without an object, leading to a TypeError when attempting to access properties on it. | ||
+ | |||
+ | ==== Cannot use global variables defined without var ==== | ||
<code javascript> | <code javascript> | ||
Line 46: | Line 62: | ||
x = 3.14; // This will throw an error because x is not declared. | x = 3.14; // This will throw an error because x is not declared. | ||
</ | </ | ||
+ | |||
+ | ==== Denial of usage of special variables / properties ==== | ||
Assigning to Read-Only Global Variables and Properties: Assigning a value to a read-only property, a getter-only property, a non-writable global variable, or a non-writable property of an object will throw an error. | Assigning to Read-Only Global Variables and Properties: Assigning a value to a read-only property, a getter-only property, a non-writable global variable, or a non-writable property of an object will throw an error. | ||
Line 53: | Line 71: | ||
undefined = " | undefined = " | ||
</ | </ | ||
+ | |||
+ | ==== Prevent the removal of variables / functions ==== | ||
Deleting Variables, Functions, or Function Parameters: Attempting to delete variables, functions, or function parameters will throw an error. | Deleting Variables, Functions, or Function Parameters: Attempting to delete variables, functions, or function parameters will throw an error. | ||
Line 61: | Line 81: | ||
delete x; // This will throw an error | delete x; // This will throw an error | ||
</ | </ | ||
+ | |||
+ | ==== Avoid duplicate parameter names ==== | ||
Duplicate Parameter Names: Duplicate parameter names in function declarations will throw an error. | Duplicate Parameter Names: Duplicate parameter names in function declarations will throw an error. | ||
Line 68: | Line 90: | ||
function x(p1, p1) {} // This will throw an error | function x(p1, p1) {} // This will throw an error | ||
</ | </ | ||
+ | |||
+ | ==== Deprecated octal syntax ==== | ||
Octal Syntax: Octal syntax in ECMAScript 5 is deprecated, and strict mode will not allow it. | Octal Syntax: Octal syntax in ECMAScript 5 is deprecated, and strict mode will not allow it. |
java-script/strict-mode.1691573632.txt.gz · Last modified: 2023/08/09 12:33 by odefta