js strict mode, and instructions


Opened a strict mode




1, the entire document open strict mode



<script>
    window.addEventListener('DOMContentLoaded',function () {
        'use strict';			//为整个脚本文档开启严格模式
         ......
    })
</script>

or

<script>
	/*
	*为这立即执行函数添加严格模式,因为这script里面只有这个立即执行函数,所以也变相
	*的给整个文档添加了严格模式
	*/
    (function () {
        'use strict';
        ......    
    }())
</script>

2, a function is turned on strict mode

<script>
    window.addEventListener('DOMContentLoaded',function () {
        function fn(){			//给这个fn函数添加严格模式
            'use strict';
            ......
        }
    })
</script>

Description Second, strict mode


1, under the strict mode variable must be declared in use
can not be under 2, strict mode arbitrarily delete variables
3, under the strict mode this point to problems

In non-strict mode this global scope of a function at the point window
and in strict mode pointing to this global scope of a function at undefined

4, under the strict mode timer this point or window
5, strict mode event, this was the caller object
6, strict mode, if the constructor without new call, this will be error ( because the global role under the strict mode this domain functions directed undefined )
. 7, in the strict mode function can not be duplicated parameters
8, in the strict mode function declarations must be at the top level, can not be declared non-functional block function
can refer to the above https: // developer .mozilla.org / zh-CN / docs / Glossary / strict_mode

Published 24 original articles · won praise 0 · Views 251

Guess you like

Origin blog.csdn.net/weixin_45969777/article/details/104877533