Вопрос: Что делает «использование строгих» в JavaScript, и каковы причины этого?


Недавно я запустил часть своего кода JavaScript через программу Crockford's JSLint , и он дал следующую ошибку:

Проблема в строке 1 символ 1: Отсутствует инструкция «use strict».

Сделав некоторые поиски, я понял, что некоторые люди добавляют "use strict";в свой код JavaScript. Как только я добавил заявление, ошибка перестала появляться. К сожалению, Google не раскрыл большую часть истории этого оператора строки. Конечно, это должно быть связано с тем, как JavaScript интерпретируется браузером, но я не знаю, каким будет эффект.

Итак, что "use strict";все о том, что это подразумевает, и все еще актуально?

Любой из текущих браузеров отвечает на "use strict";строка или это для будущего использования?


6620


источник


Ответы:


Эта статья о Javascript Strict Mode может вас заинтересовать: Джон Ресиг - Строгий режим ECMAScript 5, JSON и многое другое

Процитировать некоторые интересные части:

Строгий режим - это новая функция в ECMAScript 5, которая позволяет размещать программу или функцию в «строгом» рабочем контексте. Этот строгий контекст предотвращает принятие определенных действий и выдает больше исключений.

А также:

Строгий режим помогает в нескольких путях:

  • Он ловит некоторые распространенные кодирующие bloopers, бросая исключения.
  • Он предотвращает или создает ошибки, когда принимаются относительно «небезопасные» действия (например, получение доступа к глобальному объекту).
  • Это отключает функции, которые запутывают или плохо продумываются.

Также обратите внимание, что вы можете применить «строгий режим» ко всему файлу ... Или вы можете использовать его только для определенной функции (все еще цитируя статью Джона Ресига) :

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code... 

Что может быть полезно, если вам нужно смешать старый и новый код ;-)

Поэтому, я полагаю, это немного похоже на "use strict"вы можете использовать в Perl (отсюда и название?) : он помогает вам делать меньше ошибок, обнаруживая больше вещей, которые могут привести к поломкам.

В настоящее время поддерживается всеми основными браузерами (бар IE 9 и ниже) ,


4342



Это новая функция ECMAScript 5. Джон Ресиг написал хорошее резюме из этого.

Это просто строка, которую вы помещаете в свои файлы JavaScript (либо в верхней части файла, либо внутри функции), которая выглядит так:

"use strict";

Включение этого кода теперь не должно вызывать проблем с текущими браузерами, поскольку это всего лишь строка. Это может вызвать проблемы с вашим кодом в будущем, если ваш код нарушает прагму. Например, если у вас есть foo = "bar"без определения fooво-первых, ваш код начнет терпеть неудачу ... что, на мой взгляд, хорошо.


1087



Заявление "use strict";инструктирует браузер использовать режим Strict, который является уменьшенным и более безопасным набором функций JavaScript.

Список функций (неисчерпывающий)

  1. Запрещает глобальные переменные. (Уловы отсутствуют varобъявления и опечатки в именах переменных)

  2. Тихие неудачные назначения вызовут ошибку в строгом режиме (присвоение NaN = 5;)

  3. Попытки удалить отказоустойчивые свойства будут бросать ( delete Object.prototype)

  4. Требует, чтобы все имена свойств в объектном литерале были уникальными ( var x = {x1: "1", x1: "2"})

  5. Имена функциональных параметров должны быть уникальными ( function sum (x, x) {...})

  6. Forbids восьмеричный синтаксис ( var x = 023;некоторые разработчики ошибочно предполагают, что предыдущий нуль не делает ничего, чтобы изменить число.)

  7. Запрещает withключевое слово

  8. evalв строгом режиме не вводит новые переменные

  9. Запрет удалять простые имена ( delete x;)

  10. Запрет привязки или присвоения имен evalа также argumentsв любой форме

  11. Строгий режим не соответствует свойствам argumentsобъект с формальными параметрами. (то есть в function sum (a,b) { return arguments[0] + b;}Это работает, потому что arguments[0]связан с aи так далее. )

  12. arguments.calleeне поддерживается

[Ref: Строгий режим , Сеть разработчиков Mozilla ]


503



If people are worried about using use strict it might be worth checking out this article:

ECMAScript 5 'Strict mode' support in browsers. What does this mean?
NovoGeek.com - Krishna's weblog

It talks about browser support, but more importantly how to deal with it safely:

function isStrictMode(){
    return !this;
} 
/*
   returns false, since 'this' refers to global object and 
   '!this' becomes false
*/

function isStrictMode(){   
    "use strict";
    return !this;
} 
/* 
   returns true, since in strict mode the keyword 'this'
   does not refer to global object, unlike traditional JS. 
   So here, 'this' is 'undefined' and '!this' becomes true.
*/

362



A word of caution, all you hard-charging programmers: applying "use strict" to existing code can be hazardous! This thing is not some feel-good, happy-face sticker that you can slap on the code to make it 'better'. With the "use strict" pragma, the browser will suddenly THROW exceptions in random places that it never threw before just because at that spot you are doing something that default/loose JavaScript happily allows but strict JavaScript abhors! You may have strictness violations hiding in seldom used calls in your code that will only throw an exception when they do eventually get run - say, in the production environment that your paying customers use!

If you are going to take the plunge, it is a good idea to apply "use strict" alongside comprehensive unit tests and a strictly configured JSHint build task that will give you some confidence that there is no dark corner of your module that will blow up horribly just because you've turned on Strict Mode. Or, hey, here's another option: just don't add "use strict" to any of your legacy code, it's probably safer that way, honestly. DEFINITELY DO NOT add "use strict" to any modules you do not own or maintain, like third party modules.

I think even though it is a deadly caged animal, "use strict" can be good stuff, but you have to do it right. The best time to go strict is when your project is greenfield and you are starting from scratch. Configure JSHint/JSLint with all the warnings and options cranked up as tight as your team can stomach, get a good build/test/assert system du jour rigged like Grunt+Karma+Chai, and only THEN start marking all your new modules as "use strict". Be prepared to cure lots of niggly errors and warnings. Make sure everyone understands the gravity by configuring the build to FAIL if JSHint/JSLint produces any violations.

My project was not a greenfield project when I adopted "use strict". As a result, my IDE is full of red marks because I don't have "use strict" on half my modules, and JSHint complains about that. It's a reminder to me about what refactoring I should do in the future. My goal is to be red mark free due to all of my missing "use strict" statements, but that is years away now.


177



Using 'use strict'; does not suddenly make your code better.

The JavaScript strict mode is a feature in ECMAScript 5. You can enable the strict mode by declaring this in the top of your script/function.

'use strict';

When a JavaScript engine sees this directive, it will start to interpret the code in a special mode. In this mode, errors are thrown up when certain coding practices that could end up being potential bugs are detected (which is the reasoning behind the strict mode).

Consider this example:

var a = 365;
var b = 030;

In their obsession to line up the numeric literals, the developer has inadvertently initialized variable b with an octal literal. Non-strict mode will interpret this as a numeric literal with value 24 (in base 10). However, strict mode will throw an error.

For a non-exhaustive list of specialties in strict mode, see this answer.


Where should I use 'use strict';?

  • In my new JavaScript application: Absolutely! Strict mode can be used as a whistleblower when you are doing something stupid with your code.

  • In my existing JavaScript code: Probably not! If your existing JavaScript code has statements that are prohibited in strict-mode, the application will simply break. If you want strict mode, you should be prepared to debug and correct your existing code. This is why using 'use strict'; does not suddenly make your code better.


How do I use strict mode?

  1. Insert a 'use strict'; statement on top of your script:

    // File: myscript.js
    
    'use strict';
    var a = 2;
    ....
    

    Note that everything in the file myscript.js will be interpreted in strict mode.

  2. Or, insert a 'use strict'; statement on top of your function body:

    function doSomething() {
        'use strict';
        ...
    }
    

    Everything in the lexical scope of function doSomething will be interpreted in strict mode. The word lexical scope is important here. See this answer for a better explanation.


What things are prohibited in strict mode?

I found a nice article describing several things that are prohibited in strict mode (note that this is not an exclusive list):

Scope

Historically, JavaScript has been confused about how functions are scoped. Sometimes they seem to be statically scoped, but some features make them behave like they are dynamically scoped. This is confusing, making programs difficult to read and understand. Misunderstanding causes bugs. It also is a problem for performance. Static scoping would permit variable binding to happen at compile time, but the requirement for dynamic scope means the binding must be deferred to runtime, which comes with a significant performance penalty.

Strict mode requires that all variable binding be done statically. That means that the features that previously required dynamic binding must be eliminated or modified. Specifically, the with statement is eliminated, and the eval function’s ability to tamper with the environment of its caller is severely restricted.

One of the benefits of strict code is that tools like YUI Compressor can do a better job when processing it.

Implied Global Variables

JavaScript has implied global variables. If you do not explicitly declare a variable, a global variable is implicitly declared for you. This makes programming easier for beginners because they can neglect some of their basic housekeeping chores. But it makes the management of larger programs much more difficult and it significantly degrades reliability. So in strict mode, implied global variables are no longer created. You should explicitly declare all of your variables.

Global Leakage

There are a number of situations that could cause this to be bound to the global object. For example, if you forget to provide the new prefix when calling a constructor function, the constructor's this will be bound unexpectedly to the global object, so instead of initializing a new object, it will instead be silently tampering with global variables. In these situations, strict mode will instead bind this to undefined, which will cause the constructor to throw an exception instead, allowing the error to be detected much sooner.

Noisy Failure

JavaScript has always had read-only properties, but you could not create them yourself until ES5’s Object.createProperty function exposed that capability. If you attempted to assign a value to a read-only property, it would fail silently. The assignment would not change the property’s value, but your program would proceed as though it had. This is an integrity hazard that can cause programs to go into an inconsistent state. In strict mode, attempting to change a read-only property will throw an exception.

Octal

The octal (or base 8) representation of numbers was extremely useful when doing machine-level programming on machines whose word sizes were a multiple of 3. You needed octal when working with the CDC 6600 mainframe, which had a word size of 60 bits. If you could read octal, you could look at a word as 20 digits. Two digits represented the op code, and one digit identified one of 8 registers. During the slow transition from machine codes to high level languages, it was thought to be useful to provide octal forms in programming languages.

In C, an extremely unfortunate representation of octalness was selected: Leading zero. So in C, 0100 means 64, not 100, and 08 is an error, not 8. Even more unfortunately, this anachronism has been copied into nearly all modern languages, including JavaScript, where it is only used to create errors. It has no other purpose. So in strict mode, octal forms are no longer allowed.

Et cetera

The arguments pseudo array becomes a little bit more array-like in ES5. In strict mode, it loses its callee and caller properties. This makes it possible to pass your arguments to untrusted code without giving up a lot of confidential context. Also, the arguments property of functions is eliminated.

In strict mode, duplicate keys in a function literal will produce a syntax error. A function can’t have two parameters with the same name. A function can’t have a variable with the same name as one of its parameters. A function can’t delete its own variables. An attempt to delete a non-configurable property now throws an exception. Primitive values are not implicitly wrapped.


Reserved words for future JavaScript versions

ECMAScript 5 adds a list of reserved words. If you use them as variables or arguments, strict mode will throw an error. The reserved words are:

implements, interface, let, package, private, protected, public, static, and yield


Further Reading


126



I strongly recommend every developer to start using strict mode now. There are enough browsers supporting it that strict mode will legitimately help save us from errors we didn’t even know were in your code.

Apparently, at the initial stage there will be errors we have never encountered before. To get the full benefit, we need to do proper testing after switching to strict mode to make sure we have caught everything. Definitely we don’t just throw use strict in our code and assume there are no errors. So the churn is that it’s time to start using this incredibly useful language feature to write better code.

For example,

var person = {
    name : 'xyz',
    position : 'abc',
    fullname : function () {  "use strict"; return this.name; }
};

JSLint is a debugger written by Douglas Crockford. Simply paste in your script, and it’ll quickly scan for any noticeable issues and errors in your code.


119



I would like to offer a somewhat more founded answer complementing the other answers. I was hoping to edit the most popular answer, but failed. I tried to make it as comprehensive and complete as I could.

You can refer to the MDN documentation for more information.

"use strict" a directive introduced in ECMAScript 5.

Directives are similar to statements, yet different.

  • use strict does not contain key words: The directive is a simple expression statement, which consists of a special string literal (in single or double quotes). JavaScript engines, that do not implement ECMAScript 5, merely see an expression statement without side effects. It is expected that future versions of ECMAScript standards introduce use as a real key word; the quotes would thereby become obsolete.
  • use strict can be used only at the beginning of a script or of a function, i.e. it must precede every other (real) statement. It does not have to be the first instruction in a script of function: it can be preceded by other statement expressions that consist of string literals ( and JavaScript implementations can treat them as implementation specific directives). String literals statements, which follow a first real statement (in a script or function) are simple expression statements. Interpreters must not interpret them as directives and they have no effect.

The use strict directive indicates that the following code (in a script or a function) is strict code. The code in the highest level of a script (code that is not in a function) is considered strict code when the script contains a use strict directive. The content of a function is considered strict code when the function itself is defined in a strict code or when the function contains a use strict directive. Code that is passed to an eval() method is considered strict code when eval() was called from a strict code or contains the use strict directive itself.

The strict mode of ECMAScript 5 is a restricted subset of the JavaScript language, which eliminates relevant deficits of the language and features more stringent error checking and higher security. The following lists the differences between strict mode and normal mode (of which the first three are particularly important):

  • You cannot use the with-statement in strict mode.
  • In strict mode all variables have to be declared: if you assign a value to an identifier that has not been declared as variable, function, function parameter, catch-clause parameter or property of the global Object, then you will get a ReferenceError. In normal mode the identifier is implicitly declared as a global variable (as a property of the global Object)
  • In strict mode the keyword this has the value undefined in functions that were invoked as functions (not as methods). (In normal mode this always points to the global Object). This difference can be used to test if an implementation supports the strict mode:
var hasStrictMode = (function() { "use strict"; return this===undefined }());
  • Also when a function is invoked with call() or apply in strict mode, then this is exactly the value of the first argument of the call()or apply() invocation. (In normal mode null and undefined are replaced by the global Object and values, which are not objects, are cast into objects.)

  • In strict mode you will get a TypeError, when you try to assign to readonly properties or to define new properties for a non extensible object. (In normal mode both simply fail without error message.)

  • In strict mode, when passing code to eval(), you cannot declare or define variables or functions in the scope of the caller (as you can do it in normal mode). Instead, a new scope is created for eval() and the variables and functions are within that scope. That scope is destroyed after eval() finishes execution.
  • In strict mode the arguments-object of a function contains a static copy of the values, which are passed to that function. In normal mode the arguments-object has a somewhat "magical" behaviour: The elements of the array and the named function parameters reference both the same value.
  • In strict mode you will get a SyntaxError when the delete operator is followed by a non qualified identifier (a variable, function or function parameter). In normal mode the delete expression would do nothing and is evaluated to false.
  • In strict mode you will get a TypeError when you try to delete a non configurable property. (In normal mode the attempt simply fails and the delete expression is evaluated to false).
  • In strict mode it is considered a syntactical error when you try to define several properties with the same name for an object literal. (In normal mode there is no error.)
  • In strict mode it is considered a syntactical error when a function declaration has multiple parameters with the same name. (In normal mode there is no error.)
  • In strict mode octal literals are not allowed (these are literals that start with 0x. (In normal mode some implementations do allow octal literals.)
  • In strict mode the identifiers eval and arguments are treated like keywords. You cannot change their value, cannot assign a value to them, and you cannot use them as names for variables, functions, function parameters or identifiers of a catch block.
  • In strict mode are more restrictions on the possibilities to examine the call stack. arguments.caller and arguments.callee cause a TypeError in a function in strict mode. Furthermore, some caller- and arguments properties of functions in strict mode cause a TypeError when you try to read them.

79



My two cents:

One of the goals of strict mode is to allow for faster debugging of issues. It helps the developers by throwing exception when certain wrong things occur that can cause silent & strange behaviour of your webpage. The moment we use use strict, the code will throw out errors which helps developer to fix it in advance.

Few important things which I have learned after using use strict :

Prevents Global Variable Declaration:

var tree1Data = { name: 'Banana Tree',age: 100,leafCount: 100000};

function Tree(typeOfTree) {
    var age;
    var leafCount;

    age = typeOfTree.age;
    leafCount = typeOfTree.leafCount;
    nameoftree = typeOfTree.name;
};

var tree1 = new Tree(tree1Data);
console.log(window);

Now,this code creates nameoftree in global scope which could be accessed using window.nameoftree. When we implement use strict the code would throw error.

Uncaught ReferenceError: nameoftree is not defined

Sample

Eliminates with statement :

with statements can't be minified using tools like uglify-js. They're also deprecated and removed from future JavaScript versions.

Sample

Prevents Duplicates :

When we have duplicate property, it throws an exception

Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

"use strict";
var tree1Data = {
    name: 'Banana Tree',
    age: 100,
    leafCount: 100000,
    name:'Banana Tree'
};

There are few more but I need to gain more knowledge on that.


71



If you use a browser released in the last year or so then it most likely supports JavaScript Strict mode. Only older browsers around before ECMAScript 5 became the current standard don't support it.

The quotes around the command make sure that the code will still work in older browsers as well (although the things that generate a syntax error in strict mode will generally just cause the script to malfunction in some hard to detect way in those older browsers).


52