Understanding the ‘Use Strict’ in JavaScript – User friendly Tech help
What is ‘Use Strict’?
n
nThe “use strict” is new feature in JavaScript 1.8.5 (ECMAScript version 5) that permits us to put a program/function in a “strict mode”. It allows to write “Secure” JavaScript.n
Strict mode prevents certain actions from being taken and throws more exceptions
n
nNeed of Strict Mode
n
n
n
It catches some common coding bloopers, throwing exceptions.
n It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
n It disables features that are confusing or poorly thought out.
n
nWorking Flow of ‘Use Strict’
n
nAn ECMAScript Program syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. When processed using strict mode we have three types of ECMAScript codes:
n
- n
- strict global code
- strict eval code
- strict function code
n
n
n
n
nNote:-
n 1.Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive
n
n 2.Eval code is strict eval code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct call to the eval function that is contained in strict mode code.
n
n3.Function code that is part of a Function Declaration, Function Expression, or accessor Property Assignment is strict function code if its Function Declaration, Function Expression, or Property Assignment is contained in strict mode code or if the function code begins with a Directive Prologue that contains a Use Strict Directive.
n
n4.Function code that is supplied as the last argument to the built-in Function constructor is strict function code if the last argument is a String that when processed as a Function Body begins with a Directive Prologue that contains a Use Strict Directive.
n
n
nDeclaring Strict Mode
n
nPitch this at the beginning of a JavaScript file to enable it for the whole script (Global scope):
n
Use Strict Command |
n
Or toss it within a function to turn on strict mode only within that context (Function scope).
n
Use Strict inside a function |
n
nSyntax
n The structure/syntax, for declaring strict mode, was designed to be backward-compatible with older versions of JavaScript.
nAll of the “features” that were in earlier versions of JavaScript that were “bad syntaxes” are simply disabled (or throw errors) in strict mode.
n
n
nChanges when we use Strict Mode
n Any attempt to use a variable without declaring is not admitted.
nEg: foo=”bar” will throw an error
n
nEarlier it would assign the value to the foo property of the global object (e.g.window.foo)
n
Error in Use Strict |
n
Duplicate property in an object literal will cause an error.
n
Duplicate Property will generate Error |
n
Variable or function cannot be deleted
n
Delete Error in Use Strict |
n
Duplicate parameters are not allowed
n
Duplicate Parameter Error |
n
Deleting an undeletable property is not allowed
n
Deleting Error |
n
Any attempt to use the name ‘eval’ is strictly prohibited – as is the ability to assign the eval function to a variable or a property of an object.
n
Use of Eval as variable Name |
n
Browser Support
n
n
Browser n |
n
Version n |
n
Internet Explorer n |
n
10 n |
n
Firefox from version n |
n
4 n |
n
Chrome from version n |
n
13 n |
n
Safari from version n |
n
5.1 n |
n
Opera from version n |
n
12 n |