opentip icon indicating copy to clipboard operation
opentip copied to clipboard

Global variables polution

Open bmcgin opened this issue 11 years ago • 2 comments

It looks like opentip is setting a few global variable.

Surround the code like this:

console.log(i) /// include opentip.js console.log(i)

The variable i is getting set as a global. Can this be set to local so it will not pollute the global namespace?

Looking at the code I notice you're setting a handful of globals as: var Opentip, firstAdapter, i, mouseMoved, mousePosition, mousePositionObservers, position, vendors, _i, _len, _ref,

I am not sure if "i" needs to be a global or if can be localized. This is such a common variable that a coder may intentionally or unintentionally set his own global var "i" and clobber openTip's "i".

Ideally it would be nice if only a single global var Opentip was set.

bmcgin avatar Aug 24 '14 01:08 bmcgin

did

window.__defineSetter__('i', function () {
 throw new Error('nope!' + val)}
 );

To find it (or at least the first time it happens) on line 1534 (in the the jQuery build).

for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
position = _ref[i];
Opentip.position[position] = i;
}

This happens outside of any function, so even using var will not help. I suggest moving all the script into an IIFE.

ofirgeller avatar Mar 11 '15 10:03 ofirgeller

Here's a simple solution to the problem: Surround Opentip's entire JavaScript code with:

(function() {
// Here goes all the code ...
window.Opentip = Opentip;
})();

This will make only the Opentip object global.

TomasRiker avatar Jul 14 '17 12:07 TomasRiker