Use me strict

28
“use me strict”; !

description

Slideshow about "use strict" mode

Transcript of Use me strict

Page 1: Use me strict

“use me strict”;

!

Page 2: Use me strict

Table of contents

1. What is strict mode?

2. What does strict mode do?

3. How and can I use strict mode?

4. What does strict mode change?

5. What should you be aware using strict mode?

6. So should I use it on production?

!

Page 3: Use me strict

1. What is strict mode?

??ECMA International ECMAScript language standard

ECMA 262 standard

Strict Mode is a feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context.

This strict context prevents: - certain actions from being taken - throws more exceptions.

Page 4: Use me strict

?2. What does strict mode do?

Strict mode helps out in a couple ways:It catches some common coding bloopers, throwing exceptions.

It prevents, or throws errors, when relatively "unsafe" actions are taken.

It disables features that are confusing or poorly thought out.

Page 5: Use me strict

3. How to use strict mode?

put it at the top of document

or function before other statements:

"use strict"; or 'use strict';

"use strict";

var message = "I'm a strict mode";

Page 6: Use me strict

4.1 converting mistakes into errors

4.2 simplifying variable uses

4.3 making eval and arguments simpler

4.4 "securing" JavaScript

4.5 prepare for the future

4 What does strict mode change?

Page 7: Use me strict

4.1.1 global variables

4.1.2 NaN assigment

4.1.3 assignment to a non-writable property

4.1.4 assignment to a getter-only property

4.1.5 assignment to a new property on a non-extensible object

4.1.6 delete undeletable properties - native code

4.1.7 all properties named in an object literal be unique

4.1.8 strict mode requires that function argument names be unique

4.1.9 octal numbers serve errors

4.1 converting mistakes into errors

4.2 simplifying variable uses

4.3 making eval and arguments simpler

4.4 "securing" JavaScript

4.5 prepare for the future

4.1 Converting mistakes into errors!!

Page 8: Use me strict

4.1.1 misspelled variables"use strict";zmiennna = 10; // Uncaught ReferenceError: zmiennna is not defined

//non strictzmiennna = 10; // window.zmiennna is set

4.1.2 NaN assigment"use strict";NaN = 1; // Uncaught TypeError: // Cannot assign to read only property 'NaN' of [object Object]

//non strictNaN = 1; // no reaction

4.1 Converting mistakes into errors...!!

Page 9: Use me strict

?4.1.3 assignment to a non-writable property"use strict";var obj1 = {};Object.defineProperty(obj1, "x", { value: 1, writable: false });obj1.x = 10; // Uncaught TypeError: Cannot assign to read only property 'x' of #<Object>

//non strictobj1.x = 10; // no reaction - obj1.x is still equal to 1

4.1.4 assignment to a getter-only property"use strict";var obj2 = { get x() { return 2; } };obj2.x = 5; // Uncaught TypeError: // Cannot set property x of #<Object> which has only a getter

//non strictobj1.x = 10; // no reaction

4.1 Converting mistakes into errors...

Page 10: Use me strict

?4.1.5 assignment to a new property on a non-extensible object"use strict";var fixed = {};Object.preventExtensions(fixed);fixed.newProp = “new thing”; // Uncaught TypeError: Can't add property newProp, object is not extensible

//non strictfixed.newProp = “new thing”; // no reaction

4.1.6 delete undeletable properties from native code"use strict";delete Object.prototype; // Uncaught TypeError: // Cannot delete property 'prototype' of function Object() { [native code]

//non strictdelete Object.prototype; // no reaction

4.1 Converting mistakes into errors...

Page 11: Use me strict

4.1.7 all properties named in an object literal be unique"use strict";var o = { p: 1, p: 2 }; // Uncaught SyntaxError: // Duplicate data property in object literal not allowed in strict mode

//non stricto.p === 2 //overwritten value

4.1.8 function argument names be unique"use strict";function sum(a, a, c){ return a + b + c; }// Uncaught SyntaxError: // Strict mode function may not have duplicate parameter names //non strict - error after calling functionsum(2,3,4); // Uncaught ReferenceError: b is not defined

4.1 Converting mistakes into errors...

Page 12: Use me strict

4.1.9 octal numbers serve errors

//Actually, this effort of getting rid of octal literals comes since 1999.

parseInt("010"); // 10, ECMAScript 5 behaviorparseInt("010"); // 8, ECMAScript 3 behavior

"use strict";var sum = 011 + 120;// Uncaught SyntaxError: Octal literals are not allowed in strict mode. //non strict var sum = 011 + 120; //sum === 129

4.1 Converting mistakes into errors...

Page 13: Use me strict

4.2.1 prohibits with

4.2.2 eval sets variable which not overwrite other variables

4.2.3 forbids deleting plain names

!4.2 Simplifying variable uses!!

Page 14: Use me strict

4.2.1 prohibits with"use strict";var x = 17;with (obj) {x = 10;} // Uncaught SyntaxError: Strict mode code may not include a with statement //non strict// if obj.x does not exist global x will be overwritten

!!!

4.2 Simplifying variable uses...

Page 15: Use me strict

4.2.2 eval sets variable which not overwrite other variable"use strict";var x = 17; var evalX = eval("'use strict'; var x = 42; x");// x === 17 && evalX === 42

//non strict// x != 17 && evalX === 42

4.2.3 forbids deleting plain names"use strict";eval("var x; delete x;"); //Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.

//non strict deletes x

4.2 Simplifying variable uses...

Page 16: Use me strict

4.3.1 the names eval and arguments can't be bound or assigned in language syntax.

4.3.2 doesn't alias properties of arguments objects created within it

4.3.3 arguments.callee is no longer supported

!4.3 Making eval and arguments simpler!

!

Page 17: Use me strict

!!!

4.3.1 the names eval and arguments can't be bound or assigned in language syntax.

"use strict";eval = 17;arguments++;++eval;var obj = { set p(arguments) { } };var eval;try { } catch (arguments) { }function x(eval) { }function arguments() { }var y = function eval() { };var f = new Function("arguments", "'use strict'; return 17;");

// Uncaught SyntaxError: // Assignment to eval or arguments is not allowed in strict mode

4.3 Making eval and arguments simpler...

Page 18: Use me strict

4.3.2 doesn't alias properties of arguments objects created within it

(function (a, b) {"use strict"; arguments[0] = arguments[1]; console.log(a, b);}('one', 'two')); // one two

//non strict(function (a, b) { arguments[0] = arguments[1]; console.log(a, b);}('one', 'two')); // two two

4.3.3 arguments.callee is no longer supported// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties // may not be accessed on strict mode functions // or the arguments objects for calls to them // solution -> name your functions

4.3 Making eval and arguments simpler...

Page 19: Use me strict

4.4.1 this is undefined - global objects

4.4.2 invoking caller and arguments in function

4.4.3 arguments for functions no longer provide access to the corresponding function call's variables

4.4.4 function declared inside a statement or a block cause errors

!4.4 “Securing” JavaScript!

Page 20: Use me strict

!! 4.4.1 this is undefined - global objects

"use strict";function fun() { return this; }fun(); //undefined

//non strictfunction fun() { return this; }fun(); //window

4.4 “Securing” JavaScript...

Page 21: Use me strict

4.4.2 invoking caller and arguments in functionfunction restricted(){"use strict"; restricted.caller; restricted.arguments; }function privilegedInvoker() { return restricted();}privilegedInvoker();// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties // may not be accessed on strict mode functions // or the arguments objects for calls to them

//non strinct// we get function which was calling our function and arguments - not secure

4.4 “Securing” JavaScript...

Page 22: Use me strict

4.4.3 arguments for functions no longer provide access to the corresponding function call's variables

function fun(a, b) {“use strict"; var v = 12; return arguments.caller; }// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties // may not be accessed on strict mode functions // or the arguments objects for calls to them

4.4 “Securing” JavaScript...

Page 23: Use me strict

4.4.4 function declared inside a statement or a block cause errors

"use strict";if (true){ function f() { } f();} for (var i = 0; i < 5; i++){ function f2() { } f2();}// Uncaught SyntaxError: // In strict mode code, functions can only be declared at top level // or immediately within another function.

function baz(){ function eit() { } }

4.4 “Securing” JavaScript...

Page 24: Use me strict

You cannot use this words in other way than they will be implemented:

implements,

interface,

let,

package,

private,

protected,

public,

static,

yield.

4.5 Prepare for the future

Page 25: Use me strict

- mixing non strict with strict

(Amazon failed with that - offers were not displayed correctly)

- don’t rely on strict mode without testing

!5. What should you be aware using strict mode?

Page 26: Use me strict

Yes and No

!6. So should I use it on production ?

Page 27: Use me strict

6. So should I use it on production...

Page 28: Use me strict

THANKSHAVE A NICE THURSDAY