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 9 2254
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
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
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/
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
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/
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
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/
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.
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.
;-\ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
I posted before, but have now narrowed my problem down to this method.
At the start of the method, I test to make sure that I have a
resource, a pointer to data returned from a database. This test...
|
by: Golawala, Moiz M (GE Infrastructure) |
last post by:
Hi All,
I want to pass a value between 2 modules. Both the modules are scripts (no classes involved). Does anyone know how I can do that. For eg:
in module1
if __name__ == '__main__':...
|
by: Vasileios Zografos |
last post by:
Ok, easy question.
Not caring about the variable scope what is better (i.e. possibly in
memory allocation etc)
int someVar=0;
for (int i=0;i<1000;i++)
{
...
|
by: Oldhandandy |
last post by:
Since upgrading to .Net 2003, when I run report reports within an application
using CrystalReportViewer, I'm getting the error "Specified Cast is not
Valid". A value is being passed into the report.
|
by: Nimmi Srivastav |
last post by:
Consider two functions A and B, both of which accept a variable number
of arguments (va_start, va-arg, va_end). Is there an easy way for
arguments passed to A to be, in turn, passed to B? (For...
|
by: Andres |
last post by:
Hi all,
I have the problem to assign a variable of type object to
a specific class at runtime. I want to use a string
variable that specify the class a want to set this object.
Is something...
|
by: Mike Cooper |
last post by:
There is something about inherited classes I evidently don't know...
I wrote the following class:
Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class
There is...
|
by: DDF |
last post by:
I took this snippet from a book I foudn online. Public Class Demo
Public Structure ValueDemo
Public X As Integer
End Structure
Public Class RefDemo
Public Y As Integer
End Class
Public...
|
by: Brett Romero |
last post by:
I have methods such as the following:
void GetA20(somevar)
{
A20 = ONE
}
void GetA30(somevar)
{
A30 = ONE
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |