By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,042 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,042 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Nov 21, 4:40 pm, Marijn <marijn.huizendv...@gmail.comwrote:
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
Nov 21 '07 #2

P: n/a
On Nov 21, 6:20 pm, Darko <darko.maksimo...@gmail.comwrote:
On Nov 21, 4:40 pm, Marijn <marijn.huizendv...@gmail.comwrote:
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
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
Nov 21 '07 #3

P: n/a
Marijn said the following on 11/21/2007 1:12 PM:
On Nov 21, 6:20 pm, Darko <darko.maksimo...@gmail.comwrote:
>On Nov 21, 4:40 pm, Marijn <marijn.huizendv...@gmail.comwrote:
>>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

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');
}
Add the statement:

var someVar = 'some other value';

And then re-test your logic.
var someVar = (typeof(someVar === undefined) || typeof(someVar ===
'undefined')) ? getSomeVar() : someVar;

It evaluates someVar versus undefined. The result is a Boolean true or
false. Then it does a type of on that Boolean True or False, the result
is always "boolean". The same for the second test. So it is checking
"if(boolen && boolean){getSomeVar()}" and will always call the function
someVar and reset the value. All of that code is equivalent to this:

var someVar = "somevalue";

function getSomeVar(){
alert('getSomeVar() invoked');
return('someValue');
}
var someVar = "some other value"
var someVar = (typeof(someVar) === undefined)||
(typeof(someVar) ==='undefined') ? getSomeVar() : someVar;
alert(someVar)

Then, it will do what you are expecting/wanting it to do. Notice the
difference in where the ( ) are at.
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?
Yes, they should all call the function and re-set the value to
"somevalue". If they don't, then they are broken.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '07 #4

P: n/a
On Nov 22, 4:12 am, Marijn <marijn.huizendv...@gmail.comwrote:
On Nov 21, 4:40 pm, Marijn <marijn.huizendv...@gmail.comwrote:
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();
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 seems to be working in FF 2 an Safari 3.
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.

--
Rob
Nov 22 '07 #5

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

<snip>
>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.
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '07 #6

P: n/a
On Nov 22, 1:34 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
RobG said the following on 11/21/2007 10:02 PM:

<snip>
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.

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. :-)
--
Rob
Nov 22 '07 #7

P: n/a
RobG said the following on 11/22/2007 1:53 AM:
On Nov 22, 1:34 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>RobG said the following on 11/21/2007 10:02 PM:

<snip>
>>>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.
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'
Yes, my bad.
>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).
Yes.
>
>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. :-)
From the code, and the question, it seemed obvious to me though. If a
variable is not defined, define it. If it is defined, leave it alone. He
just had the parentheses in the wrong place to do that.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 22 '07 #8

P: n/a
In comp.lang.javascript message <5045586a-7a3f-429a-9f86-7837eb99770d@g2
1g2000hsh.googlegroups.com>, Sat, 24 Nov 2007 04:10:34, VK
<sc**********@yahoo.composted:
>
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.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Nov 24 '07 #9

P: n/a
VK
On Nov 25, 1:36 am, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
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.
;-\
Nov 24 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.