/    Sign up×
Community /Pin to ProfileBookmark

hi everyone,

can anybody tell me why the “var” is on the first line and not the second line ?

var d = new Date()
theDay=d.getDay()

and what is the significance of the “new” reserved word here ?

this is the original script:

<html>
<body>
<script type=”text/javascript”>
var d = new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write(“<b>Finally Friday</b>”)
break
case 6:
document.write(“<b>Super Saturday</b>”)
break
case 0:
document.write(“<b>Sleepy Sunday</b>”)
break
default:
document.write(“<b>I’m really looking forward to this weekend!</b>”)
}
</script>

<p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>

</body>
</html>

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@coppocksNov 21.2006 — As for the "var"... should be there. Works either way.

As for "new" ... Date() is a constructor object/function built into JavaScript, and to access those, you're supposed to use the "new" operator along with the name of its constructor. If you remove "new", notice that the date() function will not return anything.
Copy linkTweet thisAlerts:
@justinbarneskinNov 21.2006 — Sometimes the var is visible to all functions- a global variable.

Other times it's only visible inside the function where it's declared.
Copy linkTweet thisAlerts:
@changintimesauthorNov 22.2006 — and without the var it's always global, is that right ??
Copy linkTweet thisAlerts:
@KorNov 22.2006 — and without the var it's always global, is that right ??[/QUOTE]
No. Standard Javascript needs [I]always[/I] a [B]var[/B] declaration. A [I]global[/I] variable is a variable which is stated [I]outside[/I] the functions and it can be used in all the functions, while a [I]local[/I] variable is stated [I]inside[/I] a function, and it can be used only in that function (if not passed as a parameter to another function)

Global
<i>
</i>var myVar;
function setVar(){
myVar='foo';
}
...or...
var myVar='foo';

Local
<i>
</i>function setVar(){
var myVar;
myVar='foo';
}
...or...
function setVar(){
var myVar='foo'
}


Once a variable is declared, it needs no [B]var[/B] statement for further asignments

Writing a variable without a previous [B]var[/B] declaration is a corrupt syntax, and some browsers/validators will take note of this error.
Copy linkTweet thisAlerts:
@changintimesauthorNov 22.2006 — now i understand, thanks Kor,

what does "new" mean in this script ?
Copy linkTweet thisAlerts:
@KorNov 23.2006 — [B]new[/B] is an operator. It invokes an [B]object/function[/B] as a [B]constructor[/B] of native or new "classes" (in fact we are talking rather about prototypes, not classes in the common sense of the term "class" in programming launguages, but this is another discution).

For instance, [B]Date()[/B] is an object, but not a constructor:
<i>
</i>var today=Date();
alert(today)

..it will work. But when we try to apply a native "class", say [B]getDate()[/B] it will not work, as the object is not declared as a consctructor. To do that, we must call the Date() object as a consctructor, via the [B]new[/B] operator:
<i>
</i>var today= new Date();
alert(today.getDate())

To understand better the [B]new[/B] operator capabilities, see another example, when, this time, this operator will invoke a [B]function[/B] as constructor:
<i>
</i>&lt;script type="text/javascript"&gt;
function Person(firstName,lastName) {
this.first = firstName;
this.last = lastName;
}
var me = new Person('Corneliu','Rusu');
var mysister = new Person('Lucia','Rusu');
alert('I am ' +me.first+' '+me.last+' and my sister is '+mysister.first+' '+mysister.last);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@changintimesauthorNov 23.2006 — so you use the new word for a new occurance of a function or variable ??
×

Success!

Help @changintimes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...