Nick, I think that you can get away with only using the RegExp once (especially since the code you sent set the RegExp into a variable, uses that once, and then you use another RegExp literal in your return statement):
function clean(varName){ if(varName.replace(/[^\w]+/g, '') !== varName){ throw new ReferenceError(varName + ": Invalid identifier"); } return varName; } As a quick experiment I tried to use a ternary (conditional) statement for the return with the throw in the false branch but got a syntax error, as in: return (varName.replace(/[^\w]+/g, '') === varName) ? varName : throw new ReferenceError(varName + ": Invalid identifier"); I suppose that the spec stating that the false branch is supposed to an assignment expression and that throw probably doesn't return anything that might used in an assignment that this does make sense. I don't think that I'd ever want to throw like this normally, but I just tried it out for the heck of it. Danilo Celic -------- Original Message -------- > From: "Nick Morgan" <skilldr...@gmail.com> > Sent: Saturday, August 20, 2011 1:24 PM > To: jsmentors@googlegroups.com > Subject: [JSMentors] Accessing private vars in the revealing module pattern > > Hi guys > > Thought I'd share a little toy I just made, to see what you thought: > > http://jsfiddle.net/skilldrick/EjHWT/1/ > > var obj = (function () { > var private = 5; > var private2 = 10; > > function clean(varName) { > var invalidIdentifier = /[^\w]+/g; > if (varName.match(invalidIdentifier)) { > throw new ReferenceError(varName + ": Invalid identifier"); > } > return varName.replace(/[^\w]+/g, ''); > } > > function send(varName) { > return eval(clean(varName)); > } > > return { > send: send > }; > })(); > > function logResultOrLogError(func) { > try { > console.log(func()); > } catch (e) { > console.log(e); > } > } > > logResultOrLogError(function () { > return obj.send('private'); > }); > logResultOrLogError(function () { > return obj.send('blah'); > }); > logResultOrLogError(function () { > return obj.send('alert("I am teh hax0rz")'); > }); > > > It gives you access to the value of private vars via a safe eval. > Thought it might come in useful for testing occasionally. I'm not > suggesting using it in production code, just thought it was a nice > idea :) So, what do you think? > > Cheers > -- > Nick Morgan > http://skilldrick.co.uk > @skilldrick > > Save our in-boxes! http://emailcharter.org > > -- > To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com