Serious question about Variables

1

182719

Hi, can someone please explain variables and what I am doing wrong
here?

<script>

x=1;

alert(x);

</script>
 
R

RobG

Hi, can someone please explain variables and what I am doing wrong
here?

<script>

The type attribute is required:


When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).

Note that there is no block scope in javascript, and that functions
can access variables in their containing context.

alert(x);

Shows the value of x, which is 1. Since alert() is not a built-in
javascript method, strictly speaking you should test the method first
however if your code is for a web page, it is extremely unlikely that
it isn't supported:

if (typeof window.alert == 'function') {
window.alert(x);
}

but I doubt you will find anyone insisting that you do that.
 
D

David Mark

The type attribute is required:



When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).

Note that there is no block scope in javascript, and that functions
can access variables in their containing context.


Shows the value of x, which is 1. Since alert() is not a built-in
javascript method, strictly speaking you should test the method first
however if your code is for a web page, it is extremely unlikely that
it isn't supported:

if (typeof window.alert == 'function') {
window.alert(x);
}

That won't work in IE, which returns "object" for typeof(this.alert.)

You would be better off with:

if (this.alert) {
....
}

Or if you want to be overly cautious:

if (typeof this.alert == 'function' || typeof this.alert == 'object')
{
....
}
but I doubt you will find anyone insisting that you do that.

It makes sense to feature test host objects. You have no guarantee
they will exist. The second test doesn't even guarantee that alert is
callable, but it is the best you can do with host objects.
 
T

Thomas 'PointedEars' Lahn

RobG said:

When declaring variables, you should always use var. [...]

s/should/MUST/

Because it is _not_ a variable if `var' is *not* used, but a property of an
object in the scope chain. It may end up as a property of the Global Object
and so become very similar to a global variable (as the Global Object is
also the Variable Object of the global execution context); however, there
would still be difference to a global variable: this property would not have
the DontDelete attribute, and so the `delete' operator could be applied to
it successfully.

The main problem is that if `var' is not used here, the identifier may end
up as an attempt at augmentation/modification of a host object in the scope
chain, in which case anything might happen (such as runtime errors that have
been reported with the MSHTML DOM where there was an element with the same
name or ID).
Shows the value of x, which is 1. Since alert() is not a built-in
javascript method, strictly speaking you should test the method first
however if your code is for a web page, it is extremely unlikely that
it isn't supported:

if (typeof window.alert == 'function') {
window.alert(x);
}

(Most?) DOM methods, including window.alert(), yield typeof "object" in
the MSHTML DOM. So your test would yield `false' although this method is
supported there. Hence the following oft-recommended approach:

/**
* @see http://PointedEars.de/scripts/types.js
*/
function isMethodType(s)
{
return /\b(function|object)\b/i.test(s);
}

if (isMethodType(typeof window.alert) && window.alert)
{
window.alert(x);
}
but I doubt you will find anyone insisting that you do that.

There is at least one UA that would fail on calling window.alert(),
see Message-ID: <[email protected]>.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).

That "always" seems a bit too strong. I know of one seemingly-good
reason for declaring a variable, without 'var' but with initialisation,
within a function.
 
S

Steve Swift

Hi, can someone please explain variables and what I am doing wrong
here?

Well, depending upon what you expect, my guess would be that you put the
script somewhere where it does nothing.

I created a "page" with nothing but:

<H1>Script</H1>
<script>
x=1;
alert(x);
</script>

… then loaded it in my browser. I got an alert box with the message "1"
inside it. Is this what you expected?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,843
Members
47,390
Latest member
RobertMart

Latest Threads

Top