ui icon indicating copy to clipboard operation
ui copied to clipboard

strict mode

Open ron4mac opened this issue 3 years ago • 7 comments

fancybox.umd.js should be capable of running in strict mode.

ron4mac avatar May 22 '22 18:05 ron4mac

Hi,

Could you, please, explain what do you mean by that? The code is written in ES6, and ES6 modules are always in strict mode by defalt. All *.umd.js files are generated using Rollup and Babel.

fancyapps avatar May 22 '22 18:05 fancyapps

If "use strict"; is at the front of the file, it fails. fancybox.umd.js?1653245295:1 Uncaught TypeError: Cannot set property window of #<Window> which has only a getter

ron4mac avatar May 22 '22 18:05 ron4mac

Works fine for me - https://jsfiddle.net/qh3dj2gv/1/

fancyapps avatar May 22 '22 19:05 fancyapps

There, in your fiddle, "use strict" is only being applied to your snippet, not to the actual fancy box javascript file. If you don't use CDN and actually add "use strict"; to the top of the physical fancybox.umd.js file, it fails. (v4.0.25-27)

ron4mac avatar May 22 '22 20:05 ron4mac

First of all, why would you want to do that? What is your motivation? It's so weird to do that, and you should never touch files directly.

Then, your message Cannot set property window of #<Window> which has only a getter does not makes sense. I believe that it is caused by Fancybox registering self globally, so that it is available everywhere from the page. Global variables in JavaScript are attached to the "global object", which in a browser environment is aliased to window object. So, you are basically asking me to make Fancybox NOT global, but that means - you will not be able to use it at all.

fancyapps avatar May 23 '22 05:05 fancyapps

My motivation is to use scripts in my projects that pass my level of scrutiny for how well they are written. The majority of good javascript packages have "use strict"; as the first line of the file. It is a sign that the script has been fully vetted to behave properly ... especially in a mixed script environment. I use many scripts with "use strict"; at the top that have no problem attaching an object to "window".

ron4mac avatar May 23 '22 12:05 ron4mac

I have taken the time to examine the script in more detail and it seems the Fancybox object does run in strict mode. It is just the Rollup or Babel generated hoisting of the object that is breaking strict mode requirements. I can work around that for my web browser use case(s).

ron4mac avatar May 23 '22 13:05 ron4mac