Valid one time variable assign via var someVar = (this = Object) ?

M

Marijn

Hey everybody,

I was wondering if the following would be a valid JavaScript
assignment vor a variable and if anybody knows this to work in IE6 or
lower?

var someVar = (typeof(someVar) === Object) ? this : getVar();

function getVar(){
alert('getVar() called');
return(true);
}

alert(someVar);
alert(someVar);

It seems to be working in FF 2 an Safari 3.

Thanks Marijn
 
D

Darko

Hey everybody,

I was wondering if the following would be a valid JavaScript
assignment vor a variable and if anybody knows this to work in IE6 or
lower?

var someVar = (typeof(someVar) === Object) ? this : getVar();

function getVar(){
alert('getVar() called');
return(true);

}

alert(someVar);
alert(someVar);

It seems to be working in FF 2 an Safari 3.

Thanks Marijn

I think somebody forgot to include all the relevant code here.
If your someVar variable is global, as it looks from the included
code,
then how come it mentions "this"? Or is this inside a class, inside a
class-method,
or what? Please be more precise.

Regards
 
M

Marijn

I think somebody forgot to include all the relevant code here.
If your someVar variable is global, as it looks from the included
code,
then how come it mentions "this"? Or is this inside a class, inside a
class-method,
or what? Please be more precise.

Regards

First off, it's in the global scope in this case because it was a
simple example of usage. Indeed now that you say it, it doesn't make
sense to return this. The value returned should be the variable
itself.

So my refreshed example:

function getSomeVar(){
alert('getSomeVar() invoked');
return('someValue');
}

var someVar = (typeof(someVar === undefined) || typeof(someVar ===
'undefined')) ? getSomeVar() : someVar;

alert(typeof(someVar));
alert(someVar);
alert(someVar);

Is this valid, and more important do all the browsers out there
implement it as they should if it is valid?

Thanks in advance

Marijn
 
R

RobG

That is valid in terms of the syntax, although I think you are
confused about what is actually happening.

The brackets around someVar perform no useful function and give the
impression that you think typeof is a function. It isn't , it's an
operator. If your use of brackets is to make the expression clearer,
then:

var someVar = ((typeof someVar) === Object)? this : getVar();

will produce the same result and give a clearer indication of what is
happening. But since typeof always returns a string, that test will
always be false so perhaps you want:

var someVar = ((typeof someVar) === 'object')? this : getVar();

but in that case the use of === is unnecessary, so:

var someVar = ((typeof someVar) == 'object')? this : getVar();

will do the job. If someVar has not been assigned a value previously,
typeof will always return 'undefined', so the above test will always
be false. If you use:

typeof someVar == 'undefined'

it will always be true. So while the syntax is legal, the actual code
seems pointless.


[...]
It is always good to explain what you think "working" is.

[...]
So my refreshed example:

function getSomeVar(){
alert('getSomeVar() invoked');
return('someValue');

That is an unnecessary use of brackets to force evaluation of a string
literal that simply returns its value (i.e. it does nothing useful).
While the waste of a few CPU cycles is trivial, it gives the
impression that you think return is a function, whereas is it a token
that indicates the start of a return statement.

Use:

return 'someValue';

}

var someVar = (typeof(someVar === undefined) || typeof(someVar ===

As Randy said, the first test will *always* return true, the rest of
the expression is never evaluated.

[...]
Is this valid,

If you are referring to your use of the conditional operator, then
yes, it's valid. Something like JSLint will help with that:

<URL: http://www.jslint.com/ >


and more important do all the browsers out there
implement it as they should if it is valid?

All? There are probably over 100 browsers, I don't think anyone here
can answer for more than a few. The conditional operator was included
in JavaScript 1.0, I expect all browsers in use support it.
 
R

RobG

RobG said the following on 11/21/2007 10:02 PM:




Trying to explain better, not being pedantic, but, the first condition
of the ternary is what evaluates to true.

Yes, condition not test.
The first "test" converts to
Boolean true/false,
Yes.

then the typeof is boolean

typeof returns the string 'boolean'
which then gets converted
to true as a condition of the ternary operator.

Ta da! (And prevents evaluation of conditions on the other side of the
|| operator).

I think I just confused myself.

Which suggests that the whole thing could be written much better if
only the OP would say what was required. :)
 
T

Thomas 'PointedEars' Lahn

Marijn said:
I was wondering if the following would be a valid JavaScript
assignment vor a variable and if anybody knows this to work in IE6 or
lower?

var someVar = (typeof(someVar) === Object) ? this : getVar();

function getVar(){
alert('getVar() called');
return(true);
}

alert(someVar);
alert(someVar);

It seems to be working in FF 2 an Safari 3.

Why, the assignment operation certainly is valid, although it does not make
sense.

First of all, `typeof' and `return' are not method identifiers, so making
those operations to appear as method calls is unwise (although not
syntactically incorrect, and it does not change the semantics of the code).

Second, the `typeof' operation results in a string value for primitive
values and native objects, which `Object' is not (that is a Function object
reference instead, as `Object' is the identifier of a built-in constructor).
`someVar' was declared and so should have been assigned the value of
`undefined' implicitly. In a Strict Equality comparison as you performed
the result of the operation can not be equal to Object (or any other object
reference), and so `someVar' will always be assigned the return value of
getVar().

Third, given a working alert() to translate into window.alert() from DOM
Level 0, getVar() always returns `true', so `someVar' is always assigned `true'.

The only catch I can see is that, according to the MSDN Library, the Object
constructor was not supported before JScript 3.0. Hence in IE before
version 4.0 it should not be possible for the script engine to resolve this
property access (unless someone explicitly defined `Object' before) and the
script will break with an unhandled runtime error (should be a
ReferenceError exception). (It will also not compile in NN < 4, JavaScript
< 1.3, because of the Strict Equality operator, but you did not ask about that.)

The solution is not to do the test on assignment at all as "undefined"
will never be strictly equal to anything else than "undefined" or
String("undefined").


HTH

PointedEars
 
V

VK

RobG said the following on 11/21/2007 10:02 PM:




Trying to explain better, not being pedantic, but, the first condition
of the ternary is what evaluates to true. The first "test" converts to
Boolean true/false, then the typeof is boolean which then gets converted
to true as a condition of the ternary operator.

I think I just confused myself.

I can be totally off the base here, just speculating that the OP's
worries are caused by ternary operator in JavaScript and in some other
popular languages, say in VBA

ternary operator in JavaScript is "lazy": it gets evaluated to the
first positive match and the rest of statements are skept then:
var a = (b<0)? expression1 : expression2;
// if b<0 then expression2 is never evaluated.

In VBA ternary Iif is "greedy": it evaluates all statements no matter
what and only then it makes the assignment:
IIf(b<0, expression1, expression2)
' Both expression1 and expression2
' will be evaluated no matter what
' b value is

This difference once put me on the border line of craziness until I
finally decided the read the damn manual :)
 
D

Dr J R Stockton

In comp.lang.javascript message <5045586a-7a3f-429a-9f86-7837eb99770d@g2
1g2000hsh.googlegroups.com>, Sat, 24 Nov 2007 04:10:34, VK
I can be totally off the base here,
Correct.

just speculating that the OP's
worries are caused by ternary operator in JavaScript and in some other
popular languages, say in VBA

ternary operator in JavaScript is "lazy": it gets evaluated to the
first positive match and the rest of statements are skept then:
var a = (b<0)? expression1 : expression2;
In VBA ternary Iif is "greedy": it evaluates all statements no matter
what and only then it makes the assignment:
IIf(b<0, expression1, expression2)

IIf is not an operator; it is in principle a subroutine call (whatever
its authors call it), though the system may handle it in some other
manner.
 
V

VK

IIf is not an operator;
it is in principle a subroutine call (whatever
its authors call it)

In VBA it is called "function" because it has return value, as opposed
to "subroutine" that cannot return values.

This way it is easy to decide that the functional equivalent of

foobar = (x === 0) ? x : d/x;

in VBA would be:

foobar = IIf(x = 0, x, d/x)

but because of the greediness of Iif one gets a real FUBAR instead
(division on zero). Something like that caused a huge stress to me
until I finally started to read VBA Help in deep.
;-\
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top