Use strict ошибка

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. For example, if your strict code calls a function of a library that is not strict, only your code is executed in strict mode, and not the called function. 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 exhaustive 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

  • Strict Mode — JavaScript | MDN
  • Browser support for strict mode
  • Transitioning to strict mode
$(document).ready(function(){
  'use strict';
  $('#c2').hide('slow');
});
<html>
  <body>
    <div class="container">
      <div class="Header">
        <nav>
          <ul>
            <li><a href="#">Home Page</a></li>
            <li><a href="#">Kinematic Equations</a></li>
            <li><a href="#">Newtons Laws</a></li>
            <li><a href="#">Work and Force</a></li>
          </ul>
        </nav>
      </div>
    </div>

    <ul>
      <li id="one">Newtons 1st Law</li>
      <li id="two">Newtons 2nd Law</li>
      <li id="three">Newtons 3rd Law</li>
    </ul>

    <div class="content" id="c1"></div>
    <div class="content" id="c2"></div>
    <div class="content" id="c3"></div>
  </body>
</html>

When I try to run this code I do get the message «Missing ‘use strict’ statement». I am not sure what the problem could be.

When do I get this error?

The «Missing ‘use strict’ statement» error is thrown when JSLint, JSHint and
ESLint encounter a function that does not contain the strict mode directive,
and none of whose ancestor scopes contain the strict mode directive
. JSHint
will only raise this warning if the strict option is set to true. Here’s an
example of a function that does not run in strict mode:

/*jshint strict: true */
function example() {
    return true;
}

Why do I get this error?

This error is raised to highlight a lack of convention. However, as
JavaScript engines move forward, this error will increasingly be helpful as it
should highlight areas of code that may not work as you expect them to, or may
even cause fatal JavaScript errors.

A "use strict" statement is an example of a directive, which can appear as
the first statement of a program or a function (ES5 §14.1):

A Directive Prologue is the longest sequence of ExpressionStatement
productions occurring as the initial SourceElement productions of a
Program or FunctionBody and where each ExpressionStatement in the
sequence consists entirely of a StringLiteral token followed a semicolon.
The semicolon may appear explicitly or may be inserted by automatic semicolon
insertion. A Directive Prologue may be an empty sequence.

The "use strict" directive can be used to force the engine to conform to a
strict subset of the language, as defined in ES5 Annex C. It has
become something of a convention to run all JavaScript code in strict mode, to
avoid falling into traps that are apparent in the non-strict language. See the
previous link or the corresponding MDN article for the details of the
differences in strict mode. You can fix this error by simply adding a "use
strict"
directive to the function, or to an ancestor function:

/*jshint strict: true */
function example() {
    "use strict";
    return true;
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. This message is treated as an error by
JSHint which means you are unable to prevent it from being issued by ID.

In ESLint the rule that generates this warning is named strict. You can
disable it by setting it to 0, or enable it by setting it to 1.

Время на прочтение
4 мин

Количество просмотров 74K

В пятой редакции ECMAScript был представлен строгий режим (далее в статье Strict Mode). Strict Mode накладывает слой ограничений на JavaScript, он отгораживает вас от опасных частей языка (те части, которые есть исторически, но лучше чтобы их не было) и позволяет снизить вероятность ошибки.

Пока читал эту статью я написал 38 тестов, покрывающих все правила Strict Mode, объявленные в спецификации ES5. Вы можете посмотреть насколько ваш браузер поддерживает эти справила вот тут.

Код каждого теста представлен в конце статьи, чтобы помочь вам лучше понять спецификацию. Вы также можете выполнить тест вручную, скопируя код в консоль. Весь исходный код находится в моем репозитории.

Firefox 4 уже полностью поддерживает Strict Mode, а Chrome 11 практически полностью. Strict Mode уже не за горами — давайте изучим его подробнее!

Как включить Strict Mode?

Если добавить "use strict" в начало вашего JavaScript кода, то Strict Mode будет применен для всего кода:

"use strict";
012; //Восьмеричный литерал запрешен. Выбросит исключение SyntaxError в Strict Mode

В качестве альтернативы вы можете включить Strict Mode только в отдельной функции, добавив "use strict" в начало тела вашей функции:


012; //Нет ошибки (Strict Mode не включен глобально)
function foo() {
    "use strict";
    x=3; //Strict Mode выбросит исключение - запрещено неявно создавать глобальные переменные
}
foo(); //ReferenceError (Strict Mode включен для функции)

Наследуют ли внутренние функции Strict Mode от внешних функций?

Внутренняя функция, объявленная внутри внешней, в которой включен Strict Mode тоже будет иметь Strict Mode:


var wrapper = function(fn) {
  'use strict';
  var deleteNonConfigurable = function () {
    var obj = {};
    Object.defineProperty(obj, "name", {
      configurable: false
    });
    delete obj.name; // Выбросит исключение TypeError в Strict Mode
  }
  return deleteNonConfigurable;
}
 
wrapper()(); //TypeError (Strict Mode включен)

Важно запомнить, что Strict Mode не распространяется на «нестрогие» (ориг. non-strict) функции, которые выполняются внутри строгой функции (или они отправлены в функцию в качестве аргументов или выполняются, используя call или apply):


var test = function(fn) {
  'use strict';
  fn();
}
 
var deleteNonConfigurable = function () {
  var obj = {};
  Object.defineProperty(obj, "name", {
    configurable: false
  });
  delete obj.name; // Выбросит исключение TypeError в Strict Mode
}
 
test(deleteNonConfigurable); //нет ошибки (Strict Mode не применялся)

Почему я не могу включить Strict Mode в консоли моего браузера?

Когда выполняешь код в консоли фаербага или в других консолях использование "use strict" вне функции не имеет силы. Это потому, что большинство консолей обрамляют ваш код в eval’ом, поэтому ваш "use strict" не является первым выражением. Это можно обойти, обрамив ваш код в замыкание (IIFE), в начало которого мы положим "use strict" (но, когда я тестировал такой способ включения Strict Mode я понял, что это довольно неудобно, особенно если работать в консоли webkit developer tools — лучше тестировать ваш код на странице):


(function() {
    "use strict";
    var a;
    var b;
    function bar() {
        x = 5; //Strict Mode выбросит исключение за попытку создания глобальной переменной
    }
    bar(); // ReferenceError (Strict Mode включен)
})();

Что произойдет если мой браузер не поддерживает Strict Mode?

Ничего. Директива "use strict" это обычное строковое выражение, которое будет проигнорировано всеми движками JavaScript, которые не поддерживают Strict Mode. Это позволяет безопасно использовать синтаксис Strict Mode во всех браузерах без каких-либо опасений, в то время когда браузеры имеющие поддержку Strict Mode будут использовать его.

Какие правила включены в Strict Mode?

Правила определены в спецификации Strict Mode и включают в себя ограничения во время «компиляции» и интерпретации (выполнения скрипта). Это вводный обзор (каждое правило я описал с примерами в следующем параграфе): ecma262-5.com/ELS5_HTML.htm#Annex_C

Синтаксические ошибки Syntax Errors

В большинстве случаев Strict Mode предотвращает выполнение подозрительного или нелегального кода в процессе загрузки. Восьмеричные числа, дубли имен переменных, некорректное использование delete и попытки сделать что-нибудь этакие с eval и ключевым словом arguments, использование with приведет к исключению SyntaxError.

Слово this

В Strict Mode объект this не будет корректироваться. Это возможно самая интересная часть Strict Mode и самая тяжелая(шокирующая) для разработчиков. Все знают, что если первый аргумент call или apply — null или undefined, то значение this выполняемой функции будет преобразование в глобальный объект (для браузеров это window).

Прямое создание глобальных переменных

Не все согласятся с этим, но непрямое создание глобального объекта почти всегда является ошибкой. В Strict Mode вам выдадут красную карточку — ReferenceError.

arguments.caller и arguments.callee

Эти «полезные свойства» (от пер. никогда не применял их) запрещены в Strict Mode. Если вы используете их в вашем кода, то Strict Mode выбросит исключение.

Объявление существующего имени объекта

Когда вы создаете объект с двумя одинаковыми ключами, то Strict Mode выбросит исключение TypeError.

Тесты

Вот исходник моих Strict Mode тестов. Каждый набор тестов снабжен комментарием, ссылающемся на часть спецификации ECMAScript, которую он тестирует. Эта версия может быть выполнена в «режиме консоли». Т.е. вы можете скопировать тест, вставить в консоль и выполнить без изменений. Этот же код, работающий в режиме «HTML» я использовал для создания тестовой страницы, которую я представил вам в начале статьи. Этот исходник с дополнительными объектами в моем github репозитории. Я уверен, что там есть пара ошибок — не стесняйтесь присылать ошибки!

От переводчика: тут в статье шел огромный кусок кода, закинул его на pastebin

Заключение

Запрещение обращение к некоторым возможностям языка для улучшения кода — вопрос спорный, давайте отложим эти споры. В защиту Strict Mode я хочу сказать, что это отличный компромисс между тотальными переменами (которые сломают обратную совместимость) и ничего не деланием (которое приведет к захламлению языка и научит разработчиков плохому).

Что ещё почитать

ECMA-262 5th Edition: The Strict Mode of ECMAScript
Asen Bozhilov: Strict tester
Таблица совместимости с ECMAScript 5, часть посвященная Strict mode. Это отличный источник, часть большой таблицы совместимости, разработанной Юрием Зайцевым (Juriy Zaytsev aka «kangax»)

От переводчика. Strict Mode поддерживают практически половина всех браузеров, кроме своих прекрасных ограничений и иммунитету к распространенным ошибкам Strict Mode дает и другие преимущества (статья mraleph). Скоро неиспользование Strict Mode станет плохим тоном (аналогично requestAnimationFrame vs setTimeout). Сейчас самое время начать эксперименты!

Синтаксическая ошибка:»use strict» not allowed in function with non-simple parameters

Message

SyntaxError: Illegal 
SyntaxError: "use strict" not allowed in function with default parameter (Firefox)
SyntaxError: "use strict" not allowed in function with rest parameter (Firefox)
SyntaxError: "use strict" not allowed in function with destructuring parameter (Firefox)
SyntaxError: 

Error type

Что пошло не так?

Examples

Function statement

В этом случае функция sum имеет параметры по умолчанию a=1 и b=2 :

function sum(a = 1, b = 2) {
  // SyntaxError: "use strict" not allowed in function with default parameter
  'use strict';
  return a + b;
}

Если функция должна быть в строгом режиме , и весь скрипт или включающая функция также может работать в строгом режиме, вы можете переместить директиву "use strict" за пределы функции:

'use strict';
function sum(a = 1, b = 2) {
  return a + b;
}

Function expression

Выражение функции может использовать еще один обходной путь:

const sum = function sum([a, b]) {
  // SyntaxError: "use strict" not allowed in function with destructuring parameter
  'use strict';
  return a + b;
};

Это может быть преобразовано в следующее выражение:

const sum = (function() {
  'use strict';
  return function sum([a, b]) {
    return a + b;
  };
})();

Arrow function

Если стрелочной функции требуется доступ к this переменной, вы можете использовать стрелочную функцию как включающую функцию:

const callback = (...args) => {
  
  'use strict';
  return this.run(args);
};

Это может быть преобразовано в следующее выражение:

const callback = (() => {
  'use strict';
  return (...args) => {
    return this.run(args);
  };
})();

See also

  • Strict mode
  • function statement
  • function expression
  • Default parameters
  • Rest parameters
  • Destructuring parameters


JavaScript

  • RangeError:счетчик повторов должен быть меньше бесконечности.

    Исключение JavaScript «количество повторений должно быть меньше бесконечности» возникает, когда метод String.prototype.repeat() используется с аргументом, который String.prototype.repeat()

  • Предупреждение:недоступный код после заявления о возврате

    Предупреждение JavaScript «недостижимый код после оператора return» возникает при использовании выражения или без точки с запятой, но включает непосредственное выражение.

  • InternalError:слишком много рекурсии

    Исключение JavaScript «слишком много рекурсии» или «превышен максимальный размер стека вызовов» возникает, когда в базовом случае отсутствует много функций.

  • ReferenceError: присвоение необъявленной переменной «x»

    Исключение только для строгого режима JavaScript «Присвоение необъявленной переменной» возникает, когда значение ReferenceError было присвоено только в строгом режиме.

Понравилась статья? Поделить с друзьями:
  • User interface application ошибка
  • Utorrent выдает ошибку предыдущий том не смонтирован
  • Usbutil ps2 ошибка
  • Used supply in use hp как исправить ошибку
  • Usbstor ошибка при удалении раздела