Comparisons between Strict and Non-Strict Patterns in JavaScript

## Notices for Strict Model

Strict mode is recommended to be set at the beginning, or within the scope. The grammatical stringent mode that appears in front of the strings when using'use strict'will be invalid, which is equivalent to no strict mode.

// var mm = 1;

'use strict';

with ({x:1})

{

console.log(x);

}

 

1. Not allowed to use with

with ({x:1})

{

console.log(x);

}



'use strict';

with ({x:1})

{

console.log(x);

}

2. All variables must be declared that assigning an undeclared variable will report a Reference Error error, instead of implicitly creating a variable.

x = 1;

console.log(x);



var x = 1;

console.log(x);

 

3. The code in Eval cannot create variables and functions in the scope of eval. Instead, create a single scope for Eval and discard it when it returns

What is eval

eval can compute a string and execute JavaScript code in it, but it is not commonly used.

eval("x = 20;y = 20;console.log(x + y);"); // 40

console.log(x); // 20

'use strict';

eval("var x = 20,y = 20;console.log(x + y);"); // 40

console.log(x); // ReferenceError

 

4. In strict mode, arguments are static copies of special objects of functions, unlike in non-strict mode, they can directly modify the parameters passed in.

function sss (x)

{

arguments[0] = 100;

console.log(x);

}

sss(); // undefined

sss(1); // 100



'use strict';

function sss (x)

{

arguments[0] = 100;

console.log(x);

}

sss(); // undefined

sss(1); // 1

 

- In both strict and non-strict modes, this is the same for the following situations.

function sss (x){

arguments[0].a = 100;

console.log(x.a);

}

sss({a:1}); // 100

 

5. Deleting an unassigned parameter in non-strict mode returns a false value. Deleting an unassigned parameter in strict mode errors Syntax Error when configurable attribute is true and TypeError when Syntax Error is fasle.

!function sss (c) {

console.log(delete c);

}(1); // false



!function sss() {

var obj = {};

Object.defineProperty(obj,{a:1},{configurable : false});

console.log(delete obj.a);

}(1); // false



'use strict';

!function sss (c) {

console.log(delete c);

}(1); // SyntaxError



!function sss() {

var obj = {};

Object.defineProperty(obj,{a:1},{configurable : false});

console.log(delete obj.a);

}(1); // TypeError

 

6. Incompatible octal mode

 

7. In the non-strict mode, this of the general function refers to null, and in the strict mode refers to the global object.

!function sss() {

console.log(this);

}(1); // undefined


'use strict';

!function sss() {

console.log(this);

}(1); // Object...

 

8. If apply/call is used, when null or undefined is passed in, this points to null or undefined, not to the global object.

 

9. Trying to modify the non-writable property (writable = false) and adding the property to the non-extensible object is a TypeError, not a neglect.

 

10. arguments.caller,arguments.callee is disabled

 

- caller is an object or function that calls its function.

- callee refers to itself, a reference to itself.

- In both cases, the value of the function is only when it is called. In other cases, the value of the function is null.

function sss() {

var obj = {};

Object.defineProperty(obj,{a:1},{writeable : false});

console.log(obj.a = 3);

console.log(arguments.callee);

console.log(sss.caller);

};



function test() {

sss();

}

test(); // [Function: sss] [Function: test]

 

Tags: Front-end Javascript Attribute

Posted on Thu, 01 Aug 2019 01:49:28 -0700 by gli