Strict Mode

Video

JavaScript Notes

JavaScript
    /**
     * strict mode vs sloppy mode
     */
    "use strict";
    // function f(){
    //   "use strict";
    // }
    
    //neverDeclared = 123; //Reference Error
    
    //var NaN = 123; // TypeError in the browser
    //var undefined = 123;
    
    //Silent Failure
    let obj = {
      a: 123,
      b: 0,
      get x() {
        return this.b;
      }
    };

    //obj.x = 47; // cannot without the setter function
    
    Object.defineProperty(obj, "c", {
      value: 456,
      writable: false
    });

    //obj.c = 789;         //silently fails when not in strict mode
    //console.log(obj.c);

    Object.seal(obj);      //.freeze() .preventExtensions()
    //delete obj.c;        //silently fails when not in strict mode
    
    //function dup(a, b, c, a) {} //SyntaxError in strict mode - duplicate parameter
    
    function f1() {}
    
    function f1(a, b) {}   //allowed. Overwriting the first function
    
    var f1 = function() {};
    f1 = function(a, b) {};
    
    let f2 = function() {};
    f2 = function(a, b) {}; //FAILS. because we used let