473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

jslint doesn't like my syntax for conditional object creation

Hi,

I have been using the following line of code to create an object called
"Serious" if it doesn't already exist.

if (Serious == null) {var Serious = {};}

This works in the scripts I use it but but www.jslint.com is not happy
with me.

Problem at line 1 character 16: Use '===' to compare with 'null'.
if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Var Serious was used before it
was declared.
if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Identifier 'Serious' already
declared as global
if (Serious == null) {var Serious = {};}

If I change to "===" then the condition is never true.

What is the correct way to create an empty object only if the object
does not exist?

Thanks,
Peter

May 22 '06 #1
44 3322
pe**********@gm ail.com wrote:
I have been using the following line of code to create
an object called "Serious" if it doesn't already exist.

if (Serious == null) {var Serious = {};}

This works in the scripts I use it but but www.jslint.com is
not happy with me.

Problem at line 1 character 16: Use '===' to compare with
'null'. if (Serious == null) {var Serious = {};}
The double equals comparison is type-converting, and equates Undefined
with Null. JSLint seems to take the stance that if you are going to
compare something with null then you should only be interested in
whether it is null or not, and so should use a comparison that telly you
precisely that.
Problem at line 1 character 27: Var Serious was used
before it was declared.
if (Serious == null) {var Serious = {};}
As the units of scoping are function bodies any variable declared within
a function body is visible to the function's entire contents, and
variable declarations anywhere within a function's immediate contents
(rather than nested functions) result in the creation of a named
property of the Variable object _prior_ to the execution of any function
body code.

This mans that:-

if (Serious == null) {var Serious = {};}

- is no different in its behaviour from:-

var Serious;
if (Serious == null) {Serious = {};}

- and given that (and particularly the fact that all declared variable
result in the creation of Variable object properties prior to function
body code execution) it is often felt that all function local variable
declarations should appear at the top of a function body's code.
Problem at line 1 character 27: Identifier 'Serious'
already declared as global
if (Serious == null) {var Serious = {};}
Declaring a function local variable will mask global variables so that
they cannot be accessed within the function body. However, this is
unlikely to be necessary or desirable, so it may be felt that if a
variable is declared globally any use of the same variable name for a
local variable should be flagged as a potential error.
If I change to "===" then the condition is never true.
No, the unassigned local variable will have the Undefined value so while
type-converting comparison will equate null with undefined strict
comparison will not.
What is the correct way to create an empty object only if
the object does not exist?


There is not really any 'correct' test, except the test that tells you
precisely what you want to know, which depends upon what you want to
know.

If the only possibilities for a declared global variable are the
undefined value or a reference to an object (and nothing else) then a
type-converting (to boolean) test will be false for the former and true
for the latter:-

if(Serious){
... //Serious is not undefined (therefor is an object reference)
}

- or:-

if(!Serious){
... //Serious is undefined (therefor is not an object reference)
}

Less efficient tests such as using - typeof - can be more
discriminating:-

if(typeof Serious == 'undefined'){
... //Serious is undefined.
}

- which can have advantages if the possibilities are less well defined.

Richard.
May 22 '06 #2
VK

pe**********@gm ail.com wrote:
I have been using the following line of code to create an object called
"Serious" if it doesn't already exist.

if (Serious == null) {var Serious = {};}

This works in the scripts I use it but but www.jslint.com is not happy
with me.

Problem at line 1 character 16: Use '===' to compare with 'null'.
if (Serious == null) {var Serious = {};}


JSLint is right what your code is wrong, but in this case it barks up
the wrong tree. You can not address a non-initialized variable unless
in try-catch block or in typeof operator.

if (Serious == null)
in order to check this statement the engine needs to calculate the
value of Serious. If Serious doesn'r exists, we are getting an attempt
to access non-initialized variable and that is the end of the story.
Try this to see it in action:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>$Templat e$</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
try {
if (Serious == null) { Serious = {}; }
}
catch(e) {
alert(e.message );
// error "Serious is not defined"
}
</script>
</head>
<body>
<p>No content</p>
</body>
</html>
The proper variant (with some respect to the common programming
practice) is:

if (typeof Serious == "undefined" ) {var Serious = {};}

or (reversed style a la Visual Studio)

if ("undefined" == typeof Serious) {var Serious = {};}

May 23 '06 #3
VK wrote:
pe**********@gm ail.com wrote:
I have been using the following line of code to create an object
called "Serious" if it doesn't already exist.

if (Serious == null) {var Serious = {};}

This works in the scripts I use it but but www.jslint.com is not
happy with me.

Problem at line 1 character 16: Use '===' to compare with 'null'.
if (Serious == null) {var Serious = {};}
JSLint is right what your code is wrong, but in this case it barks up
the wrong tree.


How would you know?
You can not address a non-initialized variable unless
in try-catch block or in typeof operator.
Bullshit. Trying to read the value a non-declared Identifier will
produce an error (unless it is the operand of the - typeof - operator)
but the Identifier in question clearly is declared (with the - var -
keyword locally and given "Problem at line 1 character 27: Identifier
'Serious' already declared as global" in the JSLint output, the same
Identifier must already be declared globally as well.
if (Serious == null)
in order to check this statement the engine needs to calculate the
value of Serious.
And that value is the undefined value (the single value of the
Undefined type).
If Serious doesn'r exists, we are getting an attempt
to access non-initialized variable and that is the end of the story. <snip>

A declared variable is implicitly initialized to Undefined during
variable instantiation.
The proper variant
How would you know?
(with some respect to the common programming
practice)
How would you know?
is:

if (typeof Serious == "undefined" ) {var Serious = {};}

<snip>

As the variable declaration is retained here (while it was omitted from
you example of error-producing code) you supposed 'issue' does not
apply, and the appropriate type of test varies with the context of the
code. However, 'common programming practice' would not declare a
variable in that context in javascript.

Richard.

May 23 '06 #4
VK

Richard Cornford wrote:
if (Serious == null) {var Serious = {};}
JSLint is right what your code is wrong, but in this case it barks up
the wrong tree.


How would you know?


And how would you?
That was a wrong question from OP in the first place, and I should skip
on it instead of "reading" imaginary code around. That should be full /
minimum working code posted, and /then/ the relevant error description.
Trying to read the value a non-declared Identifier will
produce an error (unless it is the operand of the - typeof - operator)
but the Identifier in question clearly is declared (with the - var -
keyword locally and given "Problem at line 1 character 27: Identifier
'Serious' already declared as global" in the JSLint output, the same
Identifier must already be declared globally as well.


Unless the block in question is inside function so a function level
variable with the same name is created depending on the global variable
existence. Or unless... briefly do not repeat my mistake and don't try
to restore 3rd party context by few errors. Wait until OP will post the
relevant code.

May 23 '06 #5
VK wrote:
Richard Cornford wrote:
if (Serious == null) {var Serious = {};}

JSLint is right what your code is wrong, but in this case it barks up
the wrong tree.
How would you know?


And how would you?


That is a silly question. I know because I have spent some time
learning Javascript. I have also read and understood the source code
for JSLint and discussed the motivation for its error and warning
output with its author.
That was a wrong question from OP in the first place, and I should
skip on it instead of "reading" imaginary code around. That should
be full / minimum working code posted, and /then/ the relevant error
description.
If there was not enough information to answer the question why did you
attempt to answer it instead of asking for the additional information?
But the question asked was sufficient as it provided the code that
JSLint was complaining about and sufficient of JSLint's output to fill
in the only missing code (the global declaration of the variable).
Trying to read the value a non-declared Identifier will
produce an error (unless it is the operand of the - typeof - operator)
but the Identifier in question clearly is declared (with the - var -
keyword locally and given "Problem at line 1 character 27: Identifier
'Serious' already declared as global" in the JSLint output, the same
Identifier must already be declared globally as well).


Unless


No, not "unless", the variable _was_ declared. We can see the - var -
keyword in the code.
the block in question is inside function so a function level
variable with the same name is created depending on the global
variable existence.
"Depending on the global variable existence"? There is no such thing as
a conditional variable declaration in javascript. Any occurrence of the
- var - keyword followed by an Identifier declares a variable with a
name corresponding with the Identifier in the pertinent execution
context. A variable is created for each Identifier used with - var -,
unconditionally .

This is a basic fact about the javascript language. I have no
expectation that you would know these things but I wish you would stop
making out that you know anything about javascript (or that you have
anything to teach anyone) while not understanding the basics of the
language.
Or unless... briefly do not repeat my mistake and don't try
to restore 3rd party context by few errors. Wait until OP will post the
relevant code.


As if I would take your advice on anything.

Richard.

May 23 '06 #6
VK
VK wrote:
Or unless... briefly do not repeat my mistake and don't try
to restore 3rd party context by few errors. Wait until OP will post the
relevant code.

Richard Cornford wrote: As if I would take your advice on anything.
And that's a shame because whoever doesn't take VK's advises is being
beat down publically (smile, joke, humor :-)

Go to <http://www.jslint.com/> and paste this single line of text to
validate:
if (Serious == null) {var Serious = {};}

You get these exact errors OP got:
Problem at line 1 character 16: Use '===' to compare with 'null'.

if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Var Serious was used before it was
declared.

if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Identifier 'Serious' already
declared as global

if (Serious == null) {var Serious = {};}

As I mentioned before, no one of them actually points to the real
problem (exception raised by addressing non-declared variable). JSLint
neuristic needs much more improvement on that.
The code you "restored" in your mind gives all different errors:
var Serious;
if (Serious == null) {var Serious = {};}

gives:

Problem at line 2 character 16: Use '===' to compare with 'null'.

if (Serious == null) {var Serious = {};}

Problem at line 2 character 27: Identifier 'Serious' already declared
as var*

if (Serious == null) {var Serious = {};}

That is a silly question. I know because I have spent some time
learning Javascript. I have also read and understood the source code
for JSLint and discussed the motivation for its error and warning
output with its author.
You may want to have more discussion with him after that (just don't
use the bat :-)

There is no such thing as a conditional variable declaration in javascript.


Eh?? No comments, really.

May 23 '06 #7
VK wrote:
VK wrote:
Or unless... briefly do not repeat my mistake and don't try
to restore 3rd party context by few errors. Wait until OP will post the
relevant code.
Richard Cornford wrote:
As if I would take your advice on anything.

You have quoted the above out of context and re-ordered. That is
disingenuous at minimum.
And that's a shame because whoever doesn't take VK's advises
is being beat down publically (smile, joke, humor :-)
On the contrary, disregarding anything you say will generally save
everyone a great deal of time.
Go to <http://www.jslint.com/> and paste this single line of text to
validate:
if (Serious == null) {var Serious = {};} <snip> As I mentioned before, no one of them actually points to the real
problem
I take it you don't grasp the concept of lint programs.
(exception raised by addressing non-declared variable).
How many times? There is no non-declared variable. We can see the - var
- keyword being used to declare it.
JSLint
neuristic needs much more improvement on that.
JSLint does exactly what it was designed to do.
The code you "restored" in your mind gives all different errors:
var Serious;
if (Serious == null) {var Serious = {};}


What makes you think that nonsense was in my mind?

<snip> There is no such thing as a conditional variable declaration in javascript.


Eh?? No comments, really.


There would be no point in your commenting as that is a statement of a
basic truth in javascript. As you have never really understood local
variables as a concept I would not be surprised if you did not believe
me, but that makes no difference to reality.

Richard.

May 23 '06 #8
VK

Richard Cornford wrote:
VK wrote:
(exception raised by addressing non-declared variable).
How many times? There is no non-declared variable. We can see the - var
- keyword being used to declare it.


Richard, are you trying to be stubbering up to the point to be silly?

if (Serious == null) {var Serious = {};}

In order to decide execute if-block or not, the program has to obtain
first the boolean value of the condition check (Serious == null)

In order to obtain boolean value of (Serious == null) the program has
to retrieve the value of Serious.

In order to retrieve the value of serious it has to address this
variable. If variable is not declared, it raise "Undefined indentifier"
error (error #5009 by JScript notation).

As this error raised outside of try-catch block we get uncaught error
and execution stops right there.

The execution never gets to the content of {...} so it is irrelevant
whether you have "var" there or not.
JSLint does exactly what it was designed to do.


You mean restoring the entire program out of a single line?
The code you "restored" in your mind gives all different errors:
var Serious;
if (Serious == null) {var Serious = {};}


What makes you think that nonsense was in my mind?


Because all your previous assumptions were made on that somewhere
before that line there is something like
var Serious;

If you though (like me) that Serious was never mentioned in the OP's
code before the line
if (Serious == null) {var Serious = {};}

then why did you start to argue with me?

May 23 '06 #9
VK wrote:
Richard Cornford wrote:
VK wrote:
(exception raised by addressing non-declared variable).
How many times? There is no non-declared variable. We can see the - var
- keyword being used to declare it.


Richard, are you trying to be stubbering up to the point to be silly?


Silly? I am being factually accurate; the variable is declared and we
can see that it is being declared (we, in this case, referring to
people who understand javascript, not you).
if (Serious == null) {var Serious = {};}

In order to decide execute if-block or not, the program has to obtain
first the boolean value of the condition check (Serious == null)
It does.
In order to obtain boolean value of (Serious == null) the program
has to retrieve the value of Serious.
And Serious has the undefined value (assuming that there is no
assignment to Serious prior to that point).
In order to retrieve the value of serious it has to address this
variable. If variable is not declared, it raise "Undefined indentifier"
error (error #5009 by JScript notation).
It would if Serious had not been declared, but it has so that is not an
issue.
As this error raised outside of try-catch block we get uncaught error
and execution stops right there.
Not in this case a Serious has been declared. I wonder how many times
it will be necessary for me to say this. I realise that much what I say
goes right over your head but it makes it even more obvious than is
usual from your posts that you are a fool when you do not see my being
adamant on this point as grounds for suspecting that you are wrong. But
your absolute confidence that you are never in error in what you
mistake for your understanding of javascript, is easily the biggest
barrier in your ever acquiring an understanding of javascript.
The execution never gets to the content of {...} so it is irrelevant
whether you have "var" there or not.
It is the fact that this is not true that makes conditional variable
declarations impossible in javascript. VariableDeclara tion productions
are processed during 'variable instantiation' (ECMA 262, 3rd ed.
Section 10.1.3) to create properties of the Variable object, prior to
the execution of code for the execution context. So the creation of
such properties of the Variable object cannot be influenced by control
flow inside the code as it has not been executed at the point of
'variable instantiation'.

Each and every pertinent Variable Declaration is examined during
variable instantiation and for each Identifier used a property of the
Variable object is created, regardless of the context in the code where
the - var Identifier - occurs.
JSLint does exactly what it was designed to do.


You mean restoring the entire program out of a single line?


No, I mean what I wrote.
The code you "restored" in your mind gives all different errors:
var Serious;
if (Serious == null) {var Serious = {};}


What makes you think that nonsense was in my mind?


Because all your previous assumptions were made on that
somewhere before that line there is something like
var Serious;


No they were not. Your thinking that I must have assumed that is a
product of your not understanding variable declarations in Javascript.
If you though (like me) that Serious was never mentioned in the OP's
code before the line
if (Serious == null) {var Serious = {};}

then why did you start to argue with me?


Because you posted a sequence of nonsense and untrue statements about
javascript based on your (very) personal misconceptions of the
language, as usual.

Richard.

May 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
3393
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if the need arises. If I have to go off and look it up, as I increasingly have to do with Perl's ever hairier syntax, I'm more likely to just skip it,...
12
1679
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select the type of report to generate: Select the type of report to display: <form runat="server"> <asp:DropDownList AutoPostBack="true" ID="report"...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.