Your SlideShare is downloading. ×
Use me strict
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Use me strict

1,198
views

Published on

Slideshow about "use strict" mode

Slideshow about "use strict" mode

Published in: Education, Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,198
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. “use me strict”;
  • 2. Table of contents1. 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?
  • 3. 1. What is strict mode??ECMA InternationalECMAScript language standardECMA 262 standardStrict Mode is a feature in ECMAScript 5 thatallows 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.
  • 4. 2. What does strict mode do?Strict mode helps outin 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 orpoorly thought out.
  • 5. 3. How to use strict mode?put it at the top of documentor function before other statements:"use strict"; or use strict;"use strict";var message = "Im a strict mode";
  • 6. 4.1 converting mistakes into errors4.2 simplifying variable uses4.3 making eval and arguments simpler4.4 "securing" JavaScript4.5 prepare for the future4 What does strict mode change?
  • 7. 4.1.1 global variables4.1.2 NaN assigment4.1.3 assignment to a non-writable property4.1.4 assignment to a getter-only property4.1.5 assignment to a new property on a non-extensible object4.1.6 delete undeletable properties - native code4.1.7 all properties named in an object literal be unique4.1.8 strict mode requires that function argument names be unique4.1.9 octal numbers serve errors4.1 Converting mistakes into errors!
  • 8. 4.1.1 misspelled variables"use strict";zmiennna = 10;// Uncaught ReferenceError: zmiennna is not defined//non strictzmiennna = 10; // window.zmiennna is set4.1.2 NaN assigment"use strict";NaN = 1;// Uncaught TypeError:// Cannot assign to read only property NaN of [object Object]//non strictNaN = 1; // no reaction4.1 Converting mistakes into errors...
  • 9. 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 14.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 reaction4.1 Converting mistakes into errors...
  • 10. 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: Cant add property newProp, object is not extensible//non strictfixed.newProp = “new thing”; // no reaction4.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 reaction4.1 Converting mistakes into errors...
  • 11. 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 value4.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 defined4.1 Converting mistakes into errors...
  • 12. 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 strictvar sum = 011 + 120; //sum === 1294.1 Converting mistakes into errors...
  • 13. 4.2.1 prohibits with4.2.2 eval sets variable which not overwrite other variables4.2.3 forbids deleting plain names4.2 Simplifying variable uses!
  • 14. 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 overwritten4.2 Simplifying variable uses...
  • 15. 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 === 424.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 x4.2 Simplifying variable uses...
  • 16. 4.3.1 the names eval and arguments cant be bound or assignedin language syntax.4.3.2 doesnt alias properties of arguments objects created within it4.3.3 arguments.callee is no longer supported4.3 Making eval and arguments simpler!
  • 17. 4.3.1 the names eval and arguments cant be bound or assignedin 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 mode4.3 Making eval and arguments simpler...
  • 18. 4.3.2 doesnt 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 two4.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 functions4.3 Making eval and arguments simpler...
  • 19. 4.4.1 this is undefined - global objects4.4.2 invoking caller and arguments in function4.4.3 arguments for functions no longer provide accessto the corresponding function calls variables4.4.4 function declared inside a statement or a block cause errors4.4 “Securing” JavaScript!
  • 20. 4.4.1 this is undefined - global objects"use strict";function fun() { return this; }fun(); //undefined//non strictfunction fun() { return this; }fun(); //window4.4 “Securing” JavaScript...
  • 21. 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 secure4.4 “Securing” JavaScript...
  • 22. 4.4.3 arguments for functions no longer provide accessto the corresponding function calls variablesfunction 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 them4.4 “Securing” JavaScript...
  • 23. 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...
  • 24. You cannot use this words in other waythan they will be implemented:implements,interface,let,package,private,protected,public,static,yield.4.5 Prepare for the future
  • 25. - mixing non strict with strict(Amazon failed with that - offers were not displayed correctly)- don’t rely on strict mode without testing5. What should you be aware using strict mode?
  • 26. Yes and No6. So should I use it on production ?
  • 27. 6. So should I use it on production...
  • 28. THANKSHAVE A NICE THURSDAY