eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.15k stars 467 forks source link

Some function are using global variable, which can interfere some other modules. #21

Closed jpwsutton closed 8 years ago

jpwsutton commented 8 years ago

migrated from Bugzilla #436190 status RESOLVED severity normal in component MQTT-JS for 0.9 Reported in version unspecified on platform Macintosh Assigned to: Tang Zi Han

On 2014-05-29 12:00:12 -0400, Ryosuke Tajima wrote:

Hi,

Some function are using global variables in for loop iterator. For instance, WireMessage() uses variable 'name' in for loop, which is global variable.

var WireMessage = function (type, options) {
this.type = type; for(name in options) { if (options.hasOwnProperty(name)) { this[name] = options[name]; } } };

I believe it should be:

var WireMessage = function (type, options) {
this.type = type; for(var name in options) { if (options.hasOwnProperty(name)) { this[name] = options[name]; } } };

It can interfere some other module. I have noticed this when I use mqtt31.js with google hangout APIs. After making a MQTT connection, functions of gapi.hangout.data APIs are corrupted. After I changed it to a local variable, it works fine.

I'm a newbie of MQTT and javascript. Please check I don't tell wrong.

Thanks,

On 2014-06-06 05:28:52 -0400, Al Stockdill-Mander wrote:

Thanks for reporting this, it does appear that some variables are not scoped correctly.

On 2014-07-22 10:53:19 -0400, Karl Palsson wrote:

Another place that does this is about line 200, formatting error messages

var format = function(error, substitutions) { var text = error.text; if (substitutions) { for (var i=0; i<substitutions.length; i++) { field = "{"+i+"}"; start = text.indexOf(field); if(start > 0) { var part1 = text.substring(0,start); var part2 = text.substring(start+field.length); text = part1+substitutions[i]+part2; } } } return text; };

both "field" and "start" should be made locals

On 2014-08-31 22:40:07 -0400, Tang Zi Han wrote:

Fixed in develop branch include the above two parts and some other points which are not scoped correctly.