Strict mode Flashcards
What is Strict mode in Javascript?
Strict mode allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict”; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
Give some characteristics of using Strict mode
Using a variable (incl object) without declaring it.
Deleting a function (declared as a var) is not allowed.
Duplicating a parameter name is not allowed.
Octal numeric literals are not allowed.
Escape characters are not allowed.
Writing to a read-only property is not allowed.
Writing to a get-only property is not allowed.
Deleting an undeletable property is not allowed.
The string “eval” cannnot be used as a variable.
The string “arguments” cannot be used as a variable.
The with statement is not allowed.
eval() is not allowed to create variables in the scope from which it was called.
In function calls like f(), the this value was the global object. In strict mode, it is now undefined.
In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.
What changes does strict mode make to normal JavaScript semantics?
Eliminates some JavaScript silent errors by changing them to throw errors.
Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode can sometimes be made to run faster than identical code that’s not strict mode.
Prohibits some syntax likely to be defined in future versions of ECMAScript.
Give an example of Strict mode at the script level.
// Whole-script strict mode syntax
“use strict”;
const abc = “Hi! I’m a strict mode script!”;
Give an example of Strict mode at the function level.
function strict() {
// Function-level strict mode syntax
“use strict”;
function nested() { return "And so am I!"; } return "Hi! I'm a strict mode function! " + nested(); }
function notStrict() { return “I’m not strict.”; }
Explain Strict mode for JavaScript modules.
ECMAScript 2015 introduced JavaScript modules and therefore a 3rd way to enter strict mode.
The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
function strict() {
// because this is a module, I’m strict by default
}
export default strict
How would you summarize “Changes in strict mode”?
Converting mistakes into errors
Simplifying variable uses
Making eval and arguments simpler
“Securing” JavaScript
Paving the way for future ECMAScript versions
How does Strict mode relate to “Converting mistakes into errors”?
Strict mode makes it impossible to accidentally create global variables.
Strict mode makes assignments that would otherwise silently fail to throw an exception.
Strict mode makes attempts to delete undeletable properties throw an exception.
Strict mode requires that all properties named in an object literal be unique.
Strict mode requires that function parameter names be unique.
Strict mode forbids octal syntax.
Strict mode forbids setting properties on primitive values.
How does Strict mode relate to “Simplifying variable uses”?
Strict mode prohibits with.
eval of strict mode code does not introduce new variables into the surrounding scope.
Strict mode forbids deleting plain names.
How does Strict mode relate to “Making eval and arguments simpler”?
The names eval and arguments can’t be bound or assigned in language syntax. All attempts to do so are syntax errors.
Strict mode doesn’t alias properties of arguments objects created within it.
arguments.callee is no longer supported.
How does Strict mode relate to “‘Securing’ JavaScript”?
In strict mode, the value passed as “this” to a function is not forced into being an object. The specified “this” is not boxed into an object, and if unspecified, “this” will be “undefined”. **This means, among other things, that in browsers it’s no longer possible to reference the “window” object through “this” inside a strict mode function.
In strict mode, it’s no longer possible to “walk” the JavaScript stack via commonly implemented extensions to ECMAScript.
Arguments for strict mode functions no longer provide access to the corresponding function call’s variables
How does Strict mode relate to “Paving the way for future ECMAScript versions”?
In strict mode, a short list of identifiers become reserves keywords. These words are: implements, interface, let, package, private, protected, public, static and yield.
Strict mode prohibits function statements, not at the top level of a script or function. **This prohibition isn’t strict mode proper because such function statements are an extension of basic ES5. But it is the recommendationof the ECMAScript committee, and browsers will implement it.
How does Strict mode relate to browsers?
The major browsers now implement strict mode. However, don’t blindly depend on it since there still are numerous browser version used in the wild that only have partial support for strict mode or do not support it at all (eg IE < version 10)!