最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do we need to put "use strict" in external js files if our html file already has "use st

programmeradmin2浏览0评论

Do we need to put "use strict" in external js files if our html file (which imports the external js files) already has "use strict" ?

And if our external js files do not have "use strict", are they still "strict" within a HTML file that has "use strict" ?

Example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        "use strict";
        function f() {
            // calling File1 functions (File1 does not have "use strict"; at the top)
            // are the File1 functions "strict"?
        }
    </script>
    <script src="File1.js"></script>
    <script>
        //by the way.. is it strict here ?
    </script>
</head>
<body>
</body>
</html>

Do we need to put "use strict" in external js files if our html file (which imports the external js files) already has "use strict" ?

And if our external js files do not have "use strict", are they still "strict" within a HTML file that has "use strict" ?

Example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        "use strict";
        function f() {
            // calling File1 functions (File1 does not have "use strict"; at the top)
            // are the File1 functions "strict"?
        }
    </script>
    <script src="File1.js"></script>
    <script>
        //by the way.. is it strict here ?
    </script>
</head>
<body>
</body>
</html>
Share Improve this question asked Jul 5, 2011 at 6:36 Li JuanLi Juan 1,4572 gold badges10 silver badges12 bronze badges 3
  • Why not? Are you worried about 15 bytes or so? – beatgammit Commented Jul 5, 2011 at 6:44
  • 8 you totally misunderstood my question – Li Juan Commented Jul 5, 2011 at 6:55
  • Uh, my comment was trying to probe into why you even needed to know. Put use strict everywhere to make it more portable. The 15 bytes or so isn't going to kill you, so use it for clarity's sake. – beatgammit Commented Jul 5, 2011 at 8:45
Add a comment  | 

1 Answer 1

Reset to default 22

You must put "use strict"; (or 'use strict';) at the top of each script (or function) to make them strict. In your example, the functions in File1.js will not be strict, nor will the second block. See https://developer.mozilla.org/en/JavaScript/Strict_mode#Invoking_strict_mode for details.

If this wasn't the case, using strict mode could invalidate third-party scripts that you import, so it makes sense that strictness only applies to the scripts and individual functions that you explicitly specify.

For example:

external.js:

console.log("C: This script is non-strict.");

var g = function (x) {
    console.log("g is non-strict regardless of caller.");
    return 2 * x;
};

test.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        "use strict";
        console.log("A: This script element is strict.");
        function f() {
            console.log("The strictness of a script does not affect" +
                    " the strictness of external scripts, so g is" +
                    " still non-strict when called from f.");
            return g(3);
        }
    </script>
    <script src="external.js"></script>
    <script>
        f();
        console.log("B: This script element is non-strict.")
    </script>
</head>
<body>
</body>
</html>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论