Prev: FAQ Topic - Why does simple decimal arithmetic give strange results? (2010-07-29)
Next: validate 3 fields
From: Ry Nohryb on 29 Jul 2010 09:27 On Jul 29, 1:04 pm, David Mark <dmark.cins...(a)gmail.com> wrote: > On Jul 29, 5:58 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > > > > > > > On Jul 29, 11:45 am, David Mark <dmark.cins...(a)gmail.com> wrote: > > > > On Jul 29, 5:35 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > > > > > On Jul 29, 11:22 am, David Mark <dmark.cins...(a)gmail.com> wrote: > > > > > > For one, you are looking at the wrong specifications as we are > > > > > discussing language features. > > > > > The Global Object is a language feature, the 'window' symbol is not.. > > > > Yes, exactly. And you brought it into the discussion where it had no > > > place. ;) > > > The w3 site is the right place to show you where to read that the > > 'window' symbol (not a language feature) > > You are going in circles Jorge. As I mentioned (right above) the > window object had no place in the discussion in the first place. > > > that you believe to be a host > > object (it's the object that implements the Window interface) > > Of course it is a host object. It's sure as hell not a language > feature (as you yourself just noted). As Pointy would put it: Non sequitur. The Window interface is implemented on the Global Object, not on a host object. HTH. -- Jorge.
From: Ry Nohryb on 29 Jul 2010 10:55 On Jul 29, 1:01 pm, Sean Kinsey <okin...(a)gmail.com> wrote: > > In that case the only option is > if ((new Function('return "nameOfVar" in this;'))()) { > // nameOfVar exists in the global scope > } Nice. function doesGlobalVarExist (varName) { return Function('return "'+ varName+ '" in this')(); } -- Jorge.
From: Lasse Reichstein Nielsen on 29 Jul 2010 12:42 RobG <rgqld(a)iinet.net.au> writes: > I stumbled across a function that tests if a global variable exists or > not, my version of the code is below. Other than the obvious > irrelevance of such a function (a simple typeof test should be > sufficient in every case I can imagine), and that the use of > try..catch and eval should be limited as much as possible, are there > any significant issues with it? Others have pointed out a lot of shortcommings. Generally, you have a problem with function parameters shadowing what you can see through the scope chain (e.g. try checking if "arguments" is a property of the global object?) To avoid that, don't use eval (the non-direct call to eval isn't in all browsers yet, and eval has side effects). Ok, generally, don't use eval. Using eval is a sign that you are doing something wrong[1]. Passing the name of a variable around as a string is also a sign that you are doing something wrong - you are working at two different levels at the same time. In any case, just check for the property on the global object: function doesGlobalVarExist(v) { var global = function(){return this;}(); return v in global; } (It probablt doesn't work safe in strict mode ES5) /L -- Lasse Reichstein Holst Nielsen 'Javascript frameworks is a disruptive technology'
From: Dmitry A. Soshnikov on 29 Jul 2010 15:41 On 29.07.2010 20:42, Lasse Reichstein Nielsen wrote: <snip> > > In any case, just check for the property on the global object: > > function doesGlobalVarExist(v) { > var global = function(){return this;}(); > return v in global; > } > > (It probablt doesn't work safe in strict mode ES5) > Yes, it doesn't work in strict mode ES5, and not probably but exactly (because `global' is `undefined' in this case ). You can read "Strict Mode" article mentioned by me in this thread earlier if you want. There all these cases are discussed. And only indirect `eval' call may help to get global `this' value. Dmitry.
From: David Mark on 29 Jul 2010 18:23
On Jul 29, 3:41 pm, "Dmitry A. Soshnikov" <dmitry.soshni...(a)gmail.com> wrote: > On 29.07.2010 20:42, Lasse Reichstein Nielsen wrote: > > <snip> > > > > > In any case, just check for the property on the global object: > > > function doesGlobalVarExist(v) { > > var global = function(){return this;}(); > > return v in global; > > } > > > (It probablt doesn't work safe in strict mode ES5) > > Yes, it doesn't work in strict mode ES5, and not probably but exactly > (because `global' is `undefined' in this case ). You can read "Strict > Mode" article mentioned by me in this thread earlier if you want. There > all these cases are discussed. > > And only indirect `eval' call may help to get global `this' value. > I really don't understand the worries about ES5 strict mode. Even when ES5 is widely implemented, strict mode will be completely optional. That being said, in production code, this is the best solution (and will work in anything new enough to support the - in - operator, which is virtually everything in use today):- var global = this; (function() { function isGlobal(s) { return s in global; } // App goes here })(); Everything else discussed in this thread was academic (and some of it really awful). |