Keyword

22 Nov 2023 . javascript .

this

this is a property of an execution context.

  1. If the new keyword is used when calling the function, this inside the function is a brand new object.
  2. If apply, call, or bind are used to call/create a function, this inside the function is the object that is passed in as the argument.
  3. If a function is called as a method, such as obj.method() - this is the object that the function is a property of.
  4. If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it is the window object. If in strict mode, this will be undefined instead of the global object.
  5. If multiple of the above rules apply, the rule that is higher wins and will set the this value.

null, undefined, undeclared

  • Undeclared variables are created when you assign a value to an identifier that is not previously created using var, let or const.
  • A variable that is undefined is a variable that has been declared, but not assigned a value.
  • A variable that is null will have been explicitly assigned to the null value. It represents no value and is different from undefined in the sense that it has been explicitly assigned.

Conditional(ternary) operator

function getFee(isMember) {
  return (isMember ? '$2.00' : '$10.00');
}

console.log(getFee(true));
// Expected output: "$2.00"

console.log(getFee(false));
// Expected output: "$10.00"

console.log(getFee(null));
// Expected output: "$10.00"

use strict

A statement used to enable strict mode to entire scripts or individual functions.

Advantages

  • Impossible to accidentally create global variables.
  • Make assignments which would otherwise silently fail to throw an exception.
  • Delete undeletable properties throw an exception.
  • Require function parameter names be unique.
  • this is undefined in the global context.

Disadvantages

  • Missing features some developers might be used to.
  • No more access to function.caller and function.arguments.
  • Concatenation of scripts written in different strict modes might cause issues.