D
David Mark
Are you leaving ?
Soon enough; but age before beauty...
I mean, haven't you done enough here?
Are you leaving ?
I expected better from you, El Abuelo. Every time I give you the
slightest shred of credit, you make me regret it.
We don't have to agree, Mark, so don't worry.
I saw the "This section is not normative." in bold in there. But,
there's so many that aren't normative -yet- in the browsers...
http://google.com/search?q="This+section+is+not+normative"site=w3.org
--> About 416 results
And?
Say, e.g. the timers, the navigator object, the XHRs... don't you use
them ?
Well, you shouldn't: they're not in any standard! (maybe
they've been finally standardized recently (?)).
Worry about what?
The section it refers to in the *language specification* is not
normative either.
For one, you are looking at the wrong specifications as we are
discussing language features. For two, there is no standard DOM
specification for the window object anyway.
You don't get it. Don't use something that has no formal
specification if there is a standard alternative. And certainly don't
make assumptions about host objects when you don't have to.
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.
Since when is the Global Object a host object?
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;
}
}
Yes, exactly. And you brought it into the discussion where it had no
place.
Isn't the biggest problem with this (and any eval-like solution) that
it isn't without side effects?
What if the variable you are checking contains executable code?
var foo = 'alert("foo")';
doesGlobalVarExist(foo); // alerts foo;
True.
And if the only goal here is to test if a variable has a value, then
typeof is the proper way to go (...)
ISTM that typeof does not reveal "has a value" properly because a var
that exists might hold the value undefined, and typeof would return
undefined too for a var that does not exist. As the topic of this
thread is "does global variable exist", I'd say that typeof is not the
proper solution.
The w3 site is the right place to show you where to read that the
'window' symbol (not a language feature)
that you believe to be a host
object (it's the object that implements the Window interface)
is in
fact ("for some languages, such as ECMAScript") the Global Object
("the object that provides the global namespace for script
execution"), which is not a host object -but you seem to believe it
is-.
Now, next, come and tell me that it is in IEs, so that I can ROTFLOL.
as usual, David Mark did not get anything, did he?
I already wrote about the typo, the concept is clear.
Tell e which browser does not support that insertBefore, thanks.
script = null is pointless but specially with DOM nodes and IE around
I always prefer to nullify pointers and since it does not hurt,
pointless comment from your side.
new Function is pointless as well, new before Function is a
misconception of the Function itself
the Function suggestion is bad in any case, and the eval ...
(new Function("p", "return eval(p)"))("p")
// "p", FAIL!
var arguments = 123;
(new Function("p", "return eval(p)"))("arguments");
// [object Arguments], FAIL!
You are always blind man, you rush too much with your bossy blaming
and you keep doing mistakes ( who doesn't, but no reason to pick over
everything and obvious things, this does not make you a better
developer ... just annoying one that keeps doing mistakes ... )
Once again:
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;
}
}
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.:
You are going in circles Jorge. As I mentioned (right above) the
window object had no place in the discussion in the first place.
Of course it is a host object. It's sure as hell not a language
feature (as you yourself just noted).
In that case the only option is
if ((new Function('return "nameOfVar" in this;'))()) {
// nameOfVar exists in the global scope
}
RobG said: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?
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)
On 29.07.2010 20:42, Lasse Reichstein Nielsen wrote:
<snip>
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.