0

In JavaScript, if you declare a constructor like this:

var PMFont = function(text, font, size) {
    this.text = text;
    this.font = font;
    this.size = size;
    /*
    ...
    ton of code
    ...
    */
    x = 15;
};
var test = new PMFont('dd', 'Arial', 92);

And you create a global variable like the example above: x = 15;, is there a way to know, once your object has been created, if there are new global variables that have been created?

I've downloaded some code, and I'd like to know if there are some useless variables like in my example that stay in memory. I may run into far worse problems, for example:

imgd = ctx.getImageData(0, 0, this.baseWidth, this.baseHeight)

...gets all the data of an HTML5 canvas 2D context, and if it's not freed it takes a lot of RAM.

Olivier Pons
  • 13,972
  • 24
  • 98
  • 190
  • There are a number of options online, including [this](http://remysharp.com/2007/11/01/detect-global-variables/) – vch Aug 17 '14 at 21:14
  • I use sublimetext with jshint and I have it configured to choke and gag when I'm declaring a global like that. – ffflabs Aug 17 '14 at 21:16
  • 4
    add "use strict" to the top of the function while you debug – dandavis Aug 17 '14 at 21:18
  • Then I guess you are looking for this: http://stackoverflow.com/questions/2775384/detect-when-a-new-property-is-added-to-a-javascript-object – bitoiu Aug 18 '14 at 07:50
  • @dandavis Is it enough? If so, answer and add one or two links (that I will google now) and I'll validate – Olivier Pons Aug 18 '14 at 11:19

2 Answers2

0

Yes. You can check if any object has changed by storing it's map and then testing it later against a new map. This helper will do it for you:

JS

function objectWatch(object) {

    var keys = Object.keys(object);
    return {
        done: function () {
            var o = {
                created: {},
                removed: {}
            },
            newkeys = Object.keys(object);

            if (keys.length === newkeys.length) return;
            if (keys.length > newkeys.length) {
                for (var i = 0, l = keys.length; i < l; i++) {
                    if (newskeys.indexOf(keys[i]) === -1) o.removed[keys[i]] = object[keys[i]];
                }
            }
            for (var i = 0, l = newkeys.length; i < l; i++) {
                if (keys.indexOf(newkeys[i]) === -1) o.created[newkeys[i]] = object[newkeys[i]];

            }

            return o;



        }
    };
}

To use it just create an instance of it passing the object you want to watch, run your code, then call the done() method which returns and object with created and removed maps of what has changed.

check = new objectWatch(window);
x = 1;
y = 1;
console.log(check.done());

---> object: {created: {x: 1, y:1, check:object}, removed:{}}

Fiddle

http://jsfiddle.net/L6Lb35nq/

MartinWebb
  • 1,748
  • 1
  • 11
  • 12
0

Since all JS variables default to undefined, if you say the term assigned, the previous answers are not precise:

function isGloballyDefined(varName) {
   return window[varName] !== undefined 
}

If a variable has a null or false value, it already has been assigned by some piece of code.

Since you didn't specify the context, depending where you are running this code, the window object might not be available, so if you want to use this in a node context, then you probably want to look for the variable in the global object.

Hope this helps.

bitoiu
  • 5,698
  • 5
  • 33
  • 50
  • I'm not asking if it's assigned, i'm asking for a way to know if **new** global variables exist after constructing an object, and if so, which ones – Olivier Pons Aug 17 '14 at 21:50