title: Using eval and comparison's
body: <p>I'm trying to create a function that compares the value of a key in an object. I want to use the 'this' keyword to refer to the key value, however the value maybe a primitive not an object. To do this I'm using eval to evaluate the value of the the key in an if statement.</p>
  
  <p>So I can do cool stuff like this...</p>
  
  <pre><code>    bar.find({ key: 'this - 2 === 5' });
  
      bar.find({ key2: 'this.length === 11' });
  
      // In order to do this I have a function similar to this
  
      // constructor function for my object
      function foo(arg){
          this.key = 5;
          this.key2 = 'hello world';
      };
  
      // method to find if matching key values exist
      foo.prototype.find = function(args){
          // variable to hold the contents of the anonymous function
          var compare;
  
          // create an anonymous function to shield the eval from executing
          // potentially harmful code, by overwriting the document and window.
          (function(window, document){                
              compare = function(comparison){
                  if ( eval( comparison ) )
                      return true;
                  else
                      return false;
              };              
          })();
  
          // Loop over the keys in args
          for( i in args ){
              if ( this[i] ) {
                  // call the compare function bound to the scope of the
                  // keys value
                  return compare.call(this[i], args[i]);  
              }
              else {
                  return false;
              };      
          };
      };
  
      // Create an new foo object
  
      bar = new foo;
  
      // test it, should return true
  
      bar.find({key: 'this - 2 === 3', key2: 'this.length === 11'});
      &gt;&gt; false // 5 - 2 === 3
      &gt;&gt; false // 'hello world'.length === 11
  </code></pre>
  
  <p>The issue that I'm having is not that the code doesn't work. the exactly === operator doesn't work, however the == operator works fine. </p>
  
  <p>If I do </p>
  
  <pre><code>    bar.find({key: 'this - 2 == 3', key2: 'this.length == 11'});
      &gt;&gt; true // 5 - 2 == 3
      &gt;&gt; true // 'hello world'.length == 11
  </code></pre>
  
  <p>Is this something that eval is doing, causing the types not to match?</p>
  
  <p>Or if anyone has a better alternative to achieve the same thing.</p>