2

I have this method. Is there a way to write it better? What I want to check if he trigger of analyticsTracker is available.

isTriggerAvailable: function() {
      return (
        typeof VC !== 'undefined' &&
        typeof VC.components !== 'undefined' &&
        typeof VC.components.analyticsTracker !== 'undefined' &&
        typeof VC.components.analyticsTracker.trigger !== 'undefined'
      );
    },
Kstaoo
  • 63
  • 1
  • 7

1 Answers1

0

You can use the function described here.

  function checkNested(obj /*, level1, level2, ... levelN*/) {
    var args = Array.prototype.slice.call(arguments),
        obj = args.shift();

    for (var i = 0; i < args.length; i++) {
      if (!obj.hasOwnProperty(args[i])) {
        return false;
      }
      obj = obj[args[i]];
    }
    return true;
  }

https://stackoverflow.com/a/4676258/2134720

Community
  • 1
  • 1
Ahmed Abbas
  • 952
  • 11
  • 25