enyo / opentip

Opentip is an open source javascript tooltip based on the protoype framework.
http://www.opentip.org
1.25k stars 401 forks source link

Global variables polution #117

Open bmcgin opened 10 years ago

bmcgin commented 10 years ago

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.

ofirgeller commented 9 years ago

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.

TomasRiker commented 7 years ago

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.