Home  >  Article  >  Web Front-end  >  How to use JS strict mode in your project

How to use JS strict mode in your project

php中世界最好的语言
php中世界最好的语言Original
2018-06-05 09:17:041799browse

This time I will show you how to use JS strict mode in the project. What are the precautions when using JS strict mode in the project. The following is a practical case, let's take a look.

What is strict mode?

A mode that makes JS coding more standardized and eliminates some unreasonable Javascript syntax , loose parts, reduce some weird behaviors

How to use?

Just add the following string, this This syntax can be backward compatible. If it is a javascript engine that does not support strict mode, it will be directly regarded as an unassigned string literal and will be ignored directly. The supported engine will enable strict mode

'use strict'

Explanation:

1. If used in the global scope, the entire js script will turn on this mode

2. If it is only used inside the function, then it is only enabled inside the function

function doSomething(){
 'use strict' 
 // 其他代码 
}

Variable

1. What is in strict mode? There are restrictions on when and how to create variables. First of all, accidental creation of global variables is not allowed. In non-strict mode, you can create global variables as follows, but in strict mode, this is not possible

// Undeclared variables
// Non-strict mode: create global variables
// Strict mode: throw referenceError error

2. In strict mode, for variables There are also restrictions on names. In particular, you cannot use reserved words such as implements, interface, let, package, private, etc. as variable names. If you use these variables to name them, you will get a syntax error

Object

Operation of objects in strict mode is more likely to cause errors than in non-strict mode. The following situations will cause syntax errors

1. Assigning a value to a read-only property will throw a TypeError

2. Using the delete operator on a non-configurable property will throw a TypeError

3. Adding a property to a non-extensible object will throw a TypeError

Function

In strict mode, it is required that the parameters of the named function must be unique

// 重命名参数的
// 非严格模式:没有错误,只能访问第二个参数,如果要访问第一个参数,就必须通过arguments
// 严格模式语法错误
function sum(num,num){
  //do something   
}

The behavior of the arguments object is also different in strict mode. In strict mode, modifying the value of the named parameter will also be reflected in the arguments object, but in strict mode these two values ​​​​are completely independent

function showValue(value){
 value = 'foo'
 console.log(value)
 console.log(arguments[0]) // 非严格模式 : 'foo' 严格模式 :'hello'
  
}   
showValu('hello')

Function

## In strict mode, the parameters of the function must be unique

// 重名的参数
// 非严格模式中,没有错误,只能访问第一个参数
// 严格模式 :抛出错误 Uncaught SyntaxError: Duplicate parameter name not allowed in this context
function sum(num,num){
  'use strict'
  // do something 
}

arguments object

In non-strict mode, modifying the value of the named parameter will also be reflected in the arguments object, but in strict mode, the two values ​​are completely independent

// 修改命名参数的值
// 非严格模式:修改会反应到arguments中
// 严格模式中不会反应到arguments中
function sum(num,num2){
  'use strict'
num=3
  console.log(arguments[0],num2)// 严格模式下输出为1,2 非严格模式下输出为3,2
}
sum(1,2)

eval()

The biggest change of the eval function is that it no longer creates variables or functions in the containing context:

// 使用eval函数创建变量
// 非严格模式中:弹出框弹窗 20
// 严格模式中:调用alert(x)时报错
function doSomething(){
  eval('var x=20')
  alert(x)
}

In strict mode, you can use eval () declare variables and functions, but these side lines or functions can only be valid in the special scope being evaluated, and will then be destroyed. There is no problem in executing the following code

'use strict'
var result=eval('x=1,y=13;x+y')
alert(result)

Here, the variables x and y are declared in eval, and then they are added together to cancel their sum, so the value of the result variable is 21, which is the result of x y. When alert is called, although x and y no longer exist , the value of the result variable is still valid

Suppress this

In non-strict mode

Use the apply( of function ) or call() method, null and undefined values ​​will be converted to global objects, and in strict mode, the this value of the function is always the specified value, no matter what value is specified:

// 访问属性
// 非严格模式:访问全局实行
// 严格模式:抛出错误,因为this的值是null
var color = "red";
function displayColor() {
 alert(this.color);
}
displayColor(null);

Other changes

在严格模式中with语句被抛弃掉了,在非严格模式中with语句能够改变解析标识符的路径,但在严格模式下,with语句被简化掉了,因此,在严格模式下使用with语句是导致语法错误

// with语句
// 非严格模式:允许
// 严格模式:抛出语法错误
with (location) {
 console.log(href);
}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读

js函数项目中常用方法总结

怎样使用原生js做出满天星效果

The above is the detailed content of How to use JS strict mode in your project. 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