Home >Web Front-end >JS Tutorial >How to use strict mode in javascript? Usage example

How to use strict mode in javascript? Usage example

青灯夜游
青灯夜游Original
2018-11-19 16:05:162925browse

How to use strict mode in javascript? This article will give you a brief introduction to what strict mode in JavaScript means? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What does strict mode mean? What is the use?

Strict mode is a new feature in ECMAScript 5 that allows us to put a program or function into a "strict" operating context. This strict context prevents certain actions from being taken and raising further exceptions.

We need to declare "use strict"; to instruct the browser to use strict mode, which is a simplified and safer JavaScript feature set.

Benefits of using strict mode: Some changes are made to ordinary JavaScript semantics.

1. Strict mode eliminates some JavaScript silent errors by changing them to throw errors.

2. Strict mode fixes a bug that made it difficult for the JavaScript engine to perform optimizations: strict mode code can sometimes run faster than the same code in non-strict mode.

3. Strict mode prohibits the definition of certain syntaxes in future versions of ECMAScript.

4. It blocks or throws errors when taking relatively "unsafe" operations (such as gaining access to global objects).

5. It disables features that are confusing or poorly thought out.

6. Strict mode makes it easier to write "safe" JavaScript code.

How to use strict mode?

Strict mode can be used in two ways: globally across the entire script, or applied to a single function.

Note: Strict mode does not apply to block statements in {} brackets.

To invoke strict mode using

in the global scope of the entire script, enter the exact statement 'use strict'; (or 'use strict' before any other statements ';).

//全脚本严格模式语法

"use strict";
var v ="严格模式脚本!";

Note: There is a flow to this syntax: non-conflicting scripts cannot be blindly connected. Consider concatenating a strict mode script with a non-strict mode script: the whole concatenation looks strict! The opposite is also true: non-strict plus strict, and it will look non-strict. Strict mode scripts connect well to each other, and non-strict mode scripts connect well to each other. Just connecting strict and non-strict scripts is problematic. Therefore, it is recommended that you enable strict mode on a feature-by-feature basis (at least during the transition period).

Using strict mode for function calls

To call strict mode in a function, similarly set the exact statement to "use" in the function body before any other statements strict"; (or 'use strict';).

function strict(){

  //功能级严格模式语法
  "use strict";

  function nested(){
     return '在Javascript中'; 
  }

  return "严格模式函数!"+ nested();
}
function notStrict(){ 
 return "非严格模式"; 
}

Example of using strict mode:

1. In ordinary JavaScript, incorrectly entering a variable name will create a new global variable . In strict mode, this will throw an error, making it impossible to accidentally create a global variable variable. Using strict mode, it is not allowed to use an object without declaring it:

//不允许使用变量而不声明它:
"use strict";
x = 3.14; //这会导致错误

4. It is not allowed to delete variables (or objects) and functions, nor is it allowed to delete functions

//不允许使用对象而不声明它:
"use strict"
x = {p1:10,p2:20}; //这会导致错误

5, Copying of parameter names is not allowed

"use strict";
//不允许删除变量(或对象)和函数
var x = 3.14;
delete x; //这会导致错误

//也不允许删除功能
function x(p1,p2){}; 
delete x; //这会导致错误

6, octal digit literals are not allowed

“严格使用”;
函数x(p1,p1){}; //这会导致错误

7, escape characters are not allowed

"use strict";
var x = 010; //这会导致错误

8, writing is not allowed read-only Attribute

"use strict";
var x = \ 010; //这会导致错误

9. It is not allowed to write get-only attributes

"use strict";
var obj = {};
Object.defineProperty(obj,“x”,{
   value:0,writable:false
});
   obj.x = 3.14; //这会导致错误

10. It is not allowed to delete undeletable attributes

"use strict";
var obj = {
   get x(){return 0}
};
obj.x = 3.14; //这会导致错误

11. The string "eval" cannot be used As a variable

"use strict";
delete Object.prototype; //这会导致错误

12. The string "arguments" cannot be used as a variable

"use strict";
var eval = 3.14; //这会导致错误

13. The with statement is not allowed

"use strict";
var arguments = 3.14; //这会导致错误

14. For security reasons, it is not allowed eval() creates variables within the scope of calling it

"use strict";
with(Math){
  x = cos(2)
}; //这会导致错误

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related video tutorials, please visit:

JavaScriptTutorial

!

The above is the detailed content of How to use strict mode in javascript? Usage example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn