Prev: FAQ Topic - Why does simple decimal arithmetic give strange results? (2010-07-29)
Next: validate 3 fields
From: David Mark on 29 Jul 2010 04:28 On Jul 29, 3:56 am, Andrea Giammarchi <andrea.giammar...(a)gmail.com> wrote: > while in IE we have Scene missing? > > function isGlobal(what) { > return execScript('"' + what + '" in this'); > > } Why wouldn't you just use a function constructor (as described previously?) > > in ES3 we can generally use > > function isGlobal(what) { > return what in (function(){return this}()); > > } > > but since in ES5 and "use strict" this won't be the global object ... According to the bit of ES5 that Rob quoted, this is trivial to do in ES5 with global.eval. And again, why not just use the Function constructor? > > // compatible ES5 and "use strict" Is this meant to run in the global context? > var isGlobal = function (what) { > function $isGlobal(what) { Please don't use "$" as a function name prefix. > return what in context; > } > var > script = document.createElement("script"), > context = document.documentElement > ; > script.text = "this.$isGlobal=this;"; As I know we've discussed (remember Randy Webb?) this is not cross- browser code. > context.insertBefore(script, context.firstChild); Never insert scripts outside of the head or body. Why would you attempt to manipulate the DOM in such a non-standard way. Again, not cross-browser compatible. > context.removeChild(script); > script = null; That line is a waste of time. > context = $isGlobal; A function declared locally above. > delete context.$isGlobal; So, at this point your $isGlobal function has a property called $isGlobal and you want to delete it? I don't see it, nor do I see where this is supposed to be going. > context.isGlobal = (isGlobal = $isGlobal); > return isGlobal(what); I'm literally speechless. :( > > }; > Do not use this code under any circumstances.
From: David Mark on 29 Jul 2010 04:37 On Jul 29, 4:24 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > On Jul 29, 4:51 am, RobG <rg...(a)iinet.net.au> wrote: > > > > > > > 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? > > > function doesGlobalVarExist(v) { > > > try { > > eval(v); > > return true; > > > } catch(e) { > > return false; > > } > > } > > And what's with falsy values ? > > > > > Given the description of the eval function in ECMA-262 ed 3 (ES 3) § > > 15.1.2.1 and eval code in § 10.1.2, it seems that when using a > > "namespace" with functions initialised from an anonymous function it > > is impossible to use eval to run code with global scope, e.g.: > > > var zz = 'Global zz'; > > > var fn = (function() { > > var zz = 'Local zz'; > > return function(v) { > > return eval(v); > > } > > })(); > > > alert(fn('zz')); // shows "Local zz" > > > The above always resolves to the local zz because the calling context > > is placed on eval's scope chain when it is called, so the global zz is > > "shadowed". Can eval code be run as global code using this scenario? > > You never know for sure. Browser makers seem to be changing this every > day. But to be sure you could do: > > (new Function("p", "return eval(p)"))(evalInput); We covered that one, Jorge. > > > Are there cases where that is desirable (such as a > > "doesGlobalVarExist" function)? > > > There is a statement in ES5 §10.4.2 that says: > > > "...if the eval code is not being evaluated by a direct call > > (15.1.2.1.1) to the eval function then...Initialize the > > execution context as if it was a global execution context" > > > I can't see how to call eval other than as a direct call. > > var indirectEval= window.eval; > indirectEval(); Why do you insist on using that host object in lieu of the Global Object? > > > Lastly, ES 3 says: > > > "if the eval property is assigned to, an EvalError > > exception may be thrown" > > > Neither browser does, they allow assignments to eval. > > Why not: > > "k" in window > --> false > var k; > --> undefined > "k" in window > --> true > You know full well why not. :( Or do you? At the very least, you've been told a thousand times. There's no standard that says the window object *is* the Global Object. You are simply making bad inferences (from observations of a necessarily limited set of user agents). The window object may mirror global properties and setting its properties may set properties on the Global Object. It may appear in every way (that you've had time to test) to be the Global Object, but there is no way to know for sure what is going on behind the scenes with that host object and not a shred of specification anywhere that says it must behave as you assume.
From: David Mark on 29 Jul 2010 04:39 On Jul 29, 4:24 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > On Jul 29, 4:51 am, RobG <rg...(a)iinet.net.au> wrote: > > > > > > > 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? > > > function doesGlobalVarExist(v) { > > > try { > > eval(v); > > return true; > > > } catch(e) { > > return false; > > } > > } > > And what's with falsy values ? You document that the one argument is required and must be a (non- empty) string. End of story.
From: David Mark on 29 Jul 2010 04:40 On Jul 29, 4:16 am, Andrea Giammarchi <andrea.giammar...(a)gmail.com> wrote: > On Jul 29, 3:56 am, Andrea Giammarchi > > uhm, there is a typo in the global name, call it _isGlobal or the > context will be the function ... > Glad we got that cleared up. However...
From: Ry Nohryb on 29 Jul 2010 04:40
On Jul 29, 10:24 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > > And what's with falsy values ? text-decoration:line-through; -- Jorge. |