Hi guys,

I have a code that check if a certain object is a regexp by evaluating the 
expression " regexp_literal instanceof RegExp". When I evaluate the code in 
a node REPL it returns true. But when I evaluate it using a 
sandboxed-module (I use it for DI in my unit tests) the same expression 
evaluates to false.

Tracking down this odd behavior I wen't writing this 
"proof-of-concept"  code:

--[ https://gist.github.com/2560249 ]--
var reg = /^[a-z]$/;

console.log('case 01: typeof /^[a-z]$/ returns %s', typeof reg);
console.log('case 02: /^[a-z]$/ instanceof RegExp returns %s', reg 
instanceof RegExp);
console.log('case 03: /^[a-z]$/.constructor.name === "RegExp" returns %s', 
reg.constructor.name === 'RegExp'); 

var util = require('util'),
    vm = require('vm'),
    sandbox = { u: reg };

vm.runInNewContext('out1 = /^[a-z]$/ instanceof RegExp; out2 = u instanceof 
RegExp; u_name = u.constructor.name', sandbox);
console.log(util.inspect(sandbox));

When I run this code I got the following output:

  hobbes:weird ericchaves$ node app.js 
  case 01: typeof /^[a-z]$/ returns object
  case 02: /^[a-z]$/ instanceof RegExp returns true
  case 03: /^[a-z]$/.constructor.name === "RegExp" returns true
  { u: /^[a-z]$/, u_name: 'RegExp', out1: true, out2: false }
 
Why is that the out2 is false? isn't the variable U inside the sandbox a 
regexp? Couls someone help me out understanding this odd behavior?

Cheers,

Eric

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to