0

Following the example in this SO thread, I was trying to implement this functionality:

var fn = "cordova.plugins.photoLibrary.saveImage";
if (isVideo) fn = "cordova.plugins.photoLibrary.saveVideo";
window[fn](entry.toURL(), album, success, error);

This however throws the following error:

TypeError: undefined is not a function (near '...window[fn]...')

Are cordova functions not part of the global window namespace?

user1361529
  • 2,667
  • 29
  • 61
  • The answer to the post you are referring to was created back in to 2009. This feature has most likely changed as module like architectures have appeared. It's bad practice to add such functions to the global scope. I highly recommend reading this answer instead : https://stackoverflow.com/a/48354493/3589092 – kemicofa ghost Sep 24 '18 at 21:12
  • Thank you, in this specific case however, cordova already publishes its plugins in a global namespace and I'm not really adding to it. I've posted what seems to be an accepted method of array access below – user1361529 Sep 27 '18 at 11:27

1 Answers1

0

According to https://eslint.org/docs/2.0.0/rules/dot-notation, this is a valid use of array notation:

var pluginName = (condition ? "saveVideo": "saveImage");
cordova.plugins.photoLibrary[pluginName](entry.toURL(), album, success, error);
user1361529
  • 2,667
  • 29
  • 61