Strict mode enforcement in Locker Service

Locker Service automatically enforces ES5 strict mode for the JavaScript. If you write JavaScript that is not valid in strict mode, you will see unexpected errors. Let's take a look at some of the best practices that one must follow for the JavaScript code in controllers and helper files:

  • Using a variable without declaring it is not allowed. Suppose that you declare a variable as follows:
a = component.set("v.name",'Test'); 

 This is invalid; instead, use the var keyword. The correct syntax is as follows:

var a = component.set("v.name",'Test');
  • Using an object without declaring it is also not allowed:
x = {x:4, y:20};      // This will cause an error

   The fix for the preceding is as follows:

var x = {x:4, y:20};      // This will cause an error
  • Deleting a variable (or an object) is not allowed. The following is not allowed:
var x = 5;
delete x;
  • Deleting a function (or an object) is also not allowed:
function x(p1, p2) {};
delete x;
  • Duplicating a parameter name is not allowed:
function x(p1, p1) {};
  • Writing to a read-only property is not allowed:
var obj = {};
Object.defineProperty(obj, “x”, {value:0, writable:false});
obj.x = 3;
  • Writing to a get-only property is not allowed:
var obj = {get x() {return 0} };
obj.x = 3;
  • Writing to a get-only property is not allowed:
var obj = {get x() {return 0} };
obj.x = 3;

The following future reserved keywords are not allowed in strict mode:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • Yield
The eval() function evaluates JavaScript code that is represented as a string. Recently, Salesforce decided to support this inside of Locker Service, in global mode.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.254.192