What does “use strict” do in JavaScript, and what is the reasoning behind it?

前端 未结 28 3645
半阙折子戏
半阙折子戏 2020-11-21 06:05

Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error:

Problem at line 1 character 1: Missing \"use

28条回答
  •  轮回少年
    2020-11-21 06:32

    It's a new feature of ECMAScript 5. John Resig wrote up a nice summary of it.

    It's just a string you put in your JavaScript files (either at the top of your file or inside of a function) that looks like this:

    "use strict";
    

    Putting it in your code now shouldn't cause any problems with current browsers as it's just a string. It may cause problems with your code in the future if your code violates the pragma. For instance, if you currently have foo = "bar" without defining foo first, your code will start failing...which is a good thing in my opinion.

提交回复
热议问题