Strict mode Flashcards

1
Q

What is Strict mode in Javascript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give some characteristics of using Strict mode

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What changes does strict mode make to normal JavaScript semantics?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of Strict mode at the script level.

A

// Whole-script strict mode syntax
“use strict”;
const abc = “Hi! I’m a strict mode script!”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example of Strict mode at the function level.

A

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.”; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain Strict mode for JavaScript modules.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you summarize “Changes in strict mode”?

A

Converting mistakes into errors

Simplifying variable uses

Making eval and arguments simpler

“Securing” JavaScript

Paving the way for future ECMAScript versions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does Strict mode relate to “Converting mistakes into errors”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does Strict mode relate to “Simplifying variable uses”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does Strict mode relate to “Making eval and arguments simpler”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Strict mode relate to “‘Securing’ JavaScript”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Strict mode relate to “Paving the way for future ECMAScript versions”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does Strict mode relate to browsers?

A

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)!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly