jslint doesn't like my syntax for conditional object creation

Discussion in 'Javascript' started by petermichaux@gmail.com, May 22, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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.
    Richard Cornford, May 22, 2006
    #2
    1. Advertising

  3. VK Guest

    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>$Template$</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 = {};}
    VK, May 23, 2006
    #3
  4. VK wrote:
    > 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.
    Richard Cornford, May 23, 2006
    #4
  5. VK Guest

    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.
    VK, May 23, 2006
    #5
  6. 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.
    Richard Cornford, May 23, 2006
    #6
  7. VK Guest

    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.
    VK, May 23, 2006
    #7
  8. 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.
    Richard Cornford, May 23, 2006
    #8
  9. VK Guest

    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?
    VK, May 23, 2006
    #9
  10. 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. VariableDeclaration 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.
    Richard Cornford, May 23, 2006
    #10
  11. VK Guest

    Richard Cornford wrote:
    > VariableDeclaration 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'.


    OK, I admit you got me on this one - I was wrong (and ECMA specs amused
    me once again).

    For me the "variable declaration" is strictly like:

    var a = null;
    var b = '';
    var ...
    ....
    // program flow
    ....

    and that was the point I was arguing (that the variable Serious was not
    explicetly pre-declared). It did not cross my mind of this hack with
    the parser fetching all var operators and pre-declaring them.

    So presuming that
    if (Serious == null) { var Serious = {}; }

    is used outside of any function, this trick does work and it doesn't
    lead to the exception. (Within a function such check is useless as
    local Serious will be always here with value undefined).

    I guess we can adjust the mutual score (your var operator pre-fetching
    against my script tag usage :)

    But why did you decide to account this trick in the JSLint? I thought
    it was supposed to be a code validator and cleaner, not a curiosity
    store? Why did you decide to teach your users that if they are smart
    and careful enough, they can avoid pre-declaring variables in their
    code?
    VK, May 23, 2006
    #11
  12. VK wrote:
    > Richard Cornford wrote:
    > > VariableDeclaration 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'.

    >
    > OK, I admit you got me on this one


    I have "got" you on every point I have ever made in your direction, you
    are just not rational enough to comprehend that.

    >- I was wrong ...

    <snip>

    And look how much time and effort was required to convince you of just
    one basic truth about javascript. This is why the nonsense you post is
    doubly harmful, not only is it rubbish itself but the time and effort
    needed to correct you (often wasted due to your recalcitrance) is
    denied to the more deserving.

    <snip>
    > .. It did not cross my mind of this hack with
    > the parser fetching all var operators and pre-declaring them.


    I don't need to hear your latest excuse for posting nonsense. Just for
    once take the advice you are given and go and learn the basics of
    javascript _before_ trying to give advice to others on the subject.

    <snip>
    > I guess we can adjust the mutual score (your var operator
    > pre-fetching against my script tag usage :)


    No 'score' is necessary. You are a probably insane halfwit who has
    never even grasped the basics of javascript but is so deluded that you
    mistake the content of your mind for something worthwhile and in some
    way related to the subject. Nobody in their right mind would pay any
    hee to anything you had to say on any subject at all, and that will
    inevitably include me.

    > But why did you decide to account this trick in the JSLint?


    Are you asking me questions in the role of author of JSLint? I am not
    its author.

    > I thought it was supposed to be a code validator and cleaner,
    > not a curiosity store?


    I didn't think that you had grasped the role of lint programs. I won't
    attempt to explain because it would go right over your head anyway.

    > Why did you decide to teach your users


    My users?

    > that if they are smart and careful enough, they can avoid
    > pre-declaring variables in their code?


    What are you talking about now?

    Richard.
    Richard Cornford, May 23, 2006
    #12
  13. VK Guest

    VK wrote:
    > > I thought it was supposed to be a code validator and cleaner,
    > > not a curiosity store?

    <snip>
    > > that if they are smart and careful enough, they can avoid
    > > pre-declaring variables in their code?

    >


    Richard Cornford wrote:
    > What are you talking about now?


    I am not an angel but you are the most stubbering horns into wall
    person I've seen so far.

    All the thread through you were saying that the line like
    var Serious; // pre-declare for future use
    did not existed and doesn't have to exist in the original program.

    You whole point was build on the statement that the first time the
    character sequence "var Serious" occured in the source code, it was in
    the line passed through the JSLint:
    if (Serious == null) {var Serious = {};}

    You were stating that ECMA specs are covering this case (unfortunately
    they are) and that the fact that the character sequence "var Serious"
    is contained in the source code in any place is good enough to have
    Serious as undefined (rather than to get an exception on trying to use
    it).

    Fine. Right.

    Do you really state that /this/ is an Ok interpretation of the term
    "pre-declare variable in your programs"?

    <http://www.jslint.com/lint.html>
    <q>
    JavaScript allows var definitions to occur anywhere within a function.
    JSLint is more strict.
    JSLint expects that a var will be declared only once, and that it will
    be declared before it is used.
    </q>

    This code:

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

    validates without problems in JSLint, and here Serious is /pre-declared
    before the first usage/ in the common programming sense, not in the
    twisted hack you read out of ECMA.

    Now take this code for validation:

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

    Problem at line 1 character 28: Var Serious was used before it was
    declared.
    if (Serious === null) {var Serious = {};}
    Problem at line 1 character 28: Identifier 'Serious' already declared
    as global
    if (Serious === null) {var Serious = {};}

    What an f word is that? First warning sure refers to the fact that I
    did not pre-declare Serious in the normal programming way shown before.

    Second warning refers to the fact that Serious was pre-declared with
    value undefined on the parsing stage (by the fact that tokens var and
    Serious were found somewhere in the source separated by whitespace).
    Now program warns me that I re-instantiate already existing variable.

    So the variable doesn't exist (first warning) and do exist (second
    warning). Nice. Great.

    The first warning corresponds to the JSLing policy I quoted before. I
    have a sneaky suspicion who squeezed the second nonsense in. No names,
    of course...
    VK, May 23, 2006
    #13
  14. VK wrote:
    > VK wrote:
    >>> I thought it was supposed to be a code validator and cleaner,
    >>> not a curiosity store?

    > <snip>
    >>> that if they are smart and careful enough, they can avoid
    >>> pre-declaring variables in their code?

    >
    > Richard Cornford wrote:
    > > What are you talking about now?

    >
    > I am not an angel but you are the most stubbering horns
    > into wall person I've seen so far.


    Pot -> Kettle.

    > All the thread through you were saying that the line like
    > var Serious; // pre-declare for future use
    > did not existed and doesn't have to exist in the original program.


    I am not that inarticulate.

    > You whole point was build on the statement that the first time the
    > character sequence "var Serious" occured in the source code, it was in
    > the line passed through the JSLint:


    Unlikely, as I usually manage something approaching meaningful
    sentences when I make a point.

    > if (Serious == null) {var Serious = {};}
    >
    > You were stating that ECMA specs are covering this case (unfortunately
    > they are)


    It would be unfortunate if a technical specification for a programming
    language did not cover such cases.

    > and that the fact that the character sequence "var Serious"
    > is contained in the source code in any place is good enough to
    > have Serious as undefined (rather than to get an exception on
    > trying to use it).
    >
    > Fine. Right.


    Yes, basic javascript. Maybe not obvious but certainly something that
    every javascript programmer can be expected to have understood after
    their first year of seriously using the language. How long have you
    been 'using' Javascript now?

    > Do you really state that /this/ is an Ok interpretation of the term
    > "pre-declare variable in your programs"?


    There is no meaning to "pre-declared" in the context of javascript, so
    no.

    > <http://www.jslint.com/lint.html>
    > <q>
    > JavaScript allows var definitions to occur anywhere within a function.
    > JSLint is more strict.
    > JSLint expects that a var will be declared only once, and that it will
    > be declared before it is used.
    > </q>


    Did you find anything there supporting your assumption JSLint was
    "supposed to be a code validator and cleaner"?

    > This code:
    >
    > var Serious = null;
    > if (Serious === null) {Serious = {};}
    >
    > validates without problems in JSLint, and here Serious is
    > /pre-declared before the first usage/


    Not "pre-declared", just declared.

    > in the common programming sense,


    How would you know?

    > not in the twisted hack you read out of ECMA.


    That twisted hack being the well-specified behaviour exhibited by all
    ECMAScript implementations?

    > Now take this code for validation:
    >
    > if (Serious === null) {var Serious = {};}
    >
    > Problem at line 1 character 28: Var Serious was used before it was
    > declared.
    > if (Serious === null) {var Serious = {};}
    > Problem at line 1 character 28: Identifier 'Serious' already declared
    > as global
    > if (Serious === null) {var Serious = {};}
    >
    > What an f word is that? First warning sure refers to the fact
    > that I did not pre-declare Serious in the normal programming way
    > shown before.


    No, it refers to the fact that it was not declared prior to its fist
    use.

    > Second warning refers to the fact that ...

    <snip>

    The second warning is irrelevant as like many similar programs errors
    and warnings may follow as the consequence of a first but not have any
    real significance because fixing the first will resolve the conditions
    that produce them.

    > So the variable doesn't exist (first warning) and do exist (second
    > warning). Nice. Great.


    But irrelevant as soon as the first warning is corrected.

    > The first warning corresponds to the JSLing policy I quoted before. I
    > have a sneaky suspicion who squeezed the second nonsense in. No
    > names, of course...


    You have no idea what you are talking about, again.

    Richard.
    Richard Cornford, May 23, 2006
    #14
  15. VK Guest

    Richard Cornford wrote:
    > Did you find anything there supporting your assumption JSLint was
    > "supposed to be a code validator and cleaner"?


    http://www.jslint.com/lint.html>
    "JSLint, a JavaScript syntax checker and validator"

    On the rest opt-out: until your first advise to anyone in this group to
    pre-declare used variables in her/his code.

    As you don't know what does this programming term mean - if it exists
    at all, it will be explained on one of occasions mentioned above.
    VK, May 23, 2006
    #15
  16. Guest

    Guys!

    Such a battle. A previous grudge or a fresh one?

    Just to clarify, my code does not have any other declaration of
    Serious. I just put the line

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

    into www.jslint.com


    Richard Cornford wrote:
    > 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).
    >
    > It would if Serious had not been declared, but it has so that is not an
    > issue.


    Does this mean that while evaluating the condition "Serious == null"
    that the JavaScript interpreter has looked ahead to see the
    declaration? That would be very strange but I find a lot of
    JavaScript's activities very strange.

    Thanks,
    Peter
    , May 23, 2006
    #16
  17. In article <>, VK
    <> writes

    <snip>
    >not in the
    >twisted hack you read out of ECMA.

    <snip>

    There's JavaScript. There's JScript. There's ECMAScript.

    Then there's VKScript. I wish there was a separate news group for people
    who want to talk about VKScript.

    John
    --
    John Harris
    John G Harris, May 23, 2006
    #17
  18. VK wrote:
    > Richard Cornford wrote:
    >> Did you find anything there supporting your assumption
    >> JSLint was "supposed to be a code validator and cleaner"?

    >
    > http://www.jslint.com/lint.html>
    > "JSLint, a JavaScript syntax checker and validator"


    So nothing about it being a "cleaner"?

    > On the rest opt-out: until your first advise to anyone
    > in this group to pre-declare used variables in her/his
    > code.


    Using "pre-declare" is meaningless, and potentially misleading in
    javascript. Any variable declaration is acted upon prior to the
    execution of code for the execution context, and all variable
    declarations for the context are handled at the same time. So there is
    no meaning in qualifying 'declaration' or 'declare' with 'pre-', 'post-'
    or anything else. Variables are just declared, no more and no less.

    > As you don't know what does this programming term mean


    I know that it is meaningless in the context of javascript.

    > - if it exists at all, it will be explained on one
    > of occasions mentioned above.


    What are you whittering on about now?

    Richard.
    Richard Cornford, May 23, 2006
    #18
  19. VK Guest

    wrote:

    > Such a battle. A previous grudge or a fresh one?


    A previous one... This time he got me. Damn his left hook to the head
    with the Identifiers pre-processing... But I reached him couple of
    times too, did you see?
    :) joking

    > Just to clarify, my code does not have any other declaration of
    > Serious. I just put the line
    >
    > if (Serious == null) {var Serious = {};}
    >
    > into www.jslint.com


    So I was right in my guess. Really you should always spell the context
    of your question.

    > Does this mean that while evaluating the condition "Serious == null"
    > that the JavaScript interpreter has looked ahead to see the
    > declaration? That would be very strange but I find a lot of
    > JavaScript's activities very strange.


    No, it doesn't look ahead, it's even more funny as I see it. On the
    parsing stage, when the program text is initially loaded and studied
    for tokens, interpreter collects all occurences of
    [var][white-space][literal]. These [literal] are being marked as
    pre-declared with value undefined. When the script execution starts,
    these [literal] are treated as pre-declared with value undefined.

    this is why say

    <script type="text/javascript">
    if (foobar == null) {foobar = {};}
    </script>

    will cause "foobar is not defined" error but

    <script type="text/javascript">
    if (foobar == null) {var foobar = {};}
    </script>

    will be fine. The most ridiculous mechanics I ever seen, this is why it
    squeezed so hard into my mind. I guess Brendan Eich knows why did they
    do this way (one of "durt coding tolerance" things?)
    VK, May 23, 2006
    #19
  20. wrote:
    > Guys!
    >
    > Such a battle. A previous grudge or a fresh one?


    An old one. VK has a serious habit of posting fictions originating in
    his deranged mind. That results in much effort being wasted correcting
    him, so that those who cannot appreciate for themselves how very little
    he understands javascript, and how much he makes up off the top of his
    head, are not taken in and don't make the mistake of believing his
    nonsense. While other people, upon being correct, tend to learn and not
    repeat their mistakes, VK's insanity prevents him from seeing that he is
    ever wrong at all. Either he never accepts that he is wrong, and so
    repeats his nonsense indefinitely, or it takes a monumental effort to
    beat the truth into him (as in this case) and even then he tends to
    still misconceive the result.

    > Just to clarify, my code does not have any other
    > declaration of Serious. I just put the line
    >
    > if (Serious == null) {var Serious = {};}
    >
    > into www.jslint.com
    >
    > Richard Cornford wrote:
    >> Silly? I am being factually accurate; the variable is declared
    >> and we can see that it is being declared ...

    <snip>
    >> It would if Serious had not been declared, but it has so that
    >> is not an issue.

    >
    > Does this mean that while evaluating the condition
    > "Serious == null" that the JavaScript interpreter has
    > looked ahead to see the declaration?


    The interpreter has looked ahead and seen the entire code. It acts upon
    variable declarations prior to executing code for the execution context
    in question. Which means that for a function local variable the
    declaration is acted upon prior to the execution of any function body
    code. The assignment of a value to the variable does not happen until
    the assignment operation is evaluated during the execution of the
    function body code.

    > That would be very strange


    It is not that strange. Javascript's scoping units are functions so
    there is no need to delay the creation of properties of the Variable
    object; any declared variable should be visible within all of the
    function in which it is declared.

    > but I find a lot of
    > JavaScript's activities very strange.


    Like all programming languages, javascript is completely logical,
    consistent and predictable in its behaviour. That makes it entirely
    possible to completely understand javascript, so long as you are capable
    of rational thought.

    Richard.
    Richard Cornford, May 23, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Kowalzick
    Replies:
    5
    Views:
    461
    Patrick Kowalzick
    Mar 14, 2006
  2. Paul Van Delst

    newbie Q: conditional object creation

    Paul Van Delst, Jul 5, 2006, in forum: Ruby
    Replies:
    5
    Views:
    134
    Paul Van Delst
    Jul 6, 2006
  3. Douglas Crockford

    JSLINT

    Douglas Crockford, Aug 27, 2004, in forum: Javascript
    Replies:
    0
    Views:
    84
    Douglas Crockford
    Aug 27, 2004
  4. Simon  Oberhammer

    jslint and conditional catch blocks

    Simon Oberhammer, Dec 10, 2008, in forum: Javascript
    Replies:
    4
    Views:
    142
    Thomas 'PointedEars' Lahn
    Dec 11, 2008
  5. Andrew Poulos

    delete ax object and JSLint

    Andrew Poulos, May 31, 2009, in forum: Javascript
    Replies:
    10
    Views:
    203
Loading...

Share This Page