Jump to content

Three Quick Beginner's Questions


violagirl

Recommended Posts

Well, I am an old-time newbie, in the fact that I have attempted to pick up JavaScript two times previously but gave up, not out of difficulty, but because I just stopped practicing. :) So here I am at it again, and this time I'll actually take the time to ASK questions when I am confused. My preferred method is to fool around with the code (albeit simple code, at this point) until I figure it out, and then if I still don't know, to ask. So here are my three questions:1. When using the document.write() function, is it possible to use the <font> tag for formatting? I tried and it didn't work at all. I got it to work with the <p> tag and some others, but not the font tag. And are there other formatting tags like this that aren't really supported with the document.write() function? What I wrote is...

<html><head></head><body><script type="text/javascript"><!--document.write("<font size="4" face="arial" color="blue">Whee!</font>");//--></script></body></html>

and with little success, and was wondering why.2. What exactly is the word new in new Date() doing? I couldn't find any articles on it.3. How do you make a text alert box appear or not appear based on the time? I understand that my coding is inadequate, as the function gets called regardless of the time, but I can't figure out how to write it correctly, so I thought I would ask. I wrote...

<html><head><script type="text/javascript"><!--// this will make a message// appear if it is before noonvar d = new Date();var time = d.getHours();if (time < 12){   function message()    {	  alert("Hey!");   };};//--></script></head><body onload="message()"></body></html

Thanks in advance! Any help you could give me would be much appreciated!!!

Link to comment
Share on other sites

1. Yes you can, the following will not work

document.write("<font size="4" face="arial" color="blue">Whee!</font>");

it is wrong because it uses double quotes in double quotes. The follow 2 examples will work.

document.write('<font size="4" face="arial" color="blue">Whee!</font>');

document.write("<font size=\"4\" face=\"arial\" color=\"blue\">Whee!</font>");

2. Date() is an object, new is creating a new instance of the Date object in the specified variable.3. Try this

<script type="text/javascript"><!--// this will make a message// appear if it is before noonvar d = new Date();var time = d.getHours();if (time < 12){   message();}function message(){   alert("Hey!");}//--></script>

Link to comment
Share on other sites

document.write('<font size="4" face="arial" color="blue">Whee!</font>');

document.write("<font size=\"4\" face=\"arial\" color=\"blue\">Whee!</font>");

I would add a third example which helps if the string that you want to write is pretty long:
var message = '<font size="4" face="arial" color="blue">Whee!</font>';document.write(message);

Link to comment
Share on other sites

Thanks for the explanation about the double quotes! I honestly didn't know that!

2. Date() is an object, new is creating a new instance of the Date object in the specified variable.
Um... what exactly do you mean by this? So when you are using a object like that in a variable, I realize that you can't just put d = Date(); instead of new Date(); in that program and still have it work, because I tried it to see, but I'm still not fully grasping why, what this new is doing. Is it just to use these predefined objects as variables that we use the new?As for the third question, yeah, thanks, that makes perfect sense! I can't believe I didn't think of that! That's how it always seems when my questions get answered. :) But either way, thank you both very much!Megan
Link to comment
Share on other sites

To elaborate on question 2. You need to understand that there are 2 methods of programming; object oriented and procedural. Procedural is just breaking your code into chucnks and functions, the code runs in a linear fashion, like:

doFunction1();doFunction2()ldoFunction3(), etc, etc

This works well for simple scripts that don't need to repeat the same tasks alot, Object oriented is different. To make code reuse easy you would use an object (in your case the Date object). There are functions and properties assigned to the Date object in the JS core. So when you say var d = new Date() you are transferring a copy of the functions and properties in Date to d.You can also make your own objects, like:

//my objectfunction myObject(){	this.param1 = "property value";	this.func1 = function()	{	  return "function value";	}}//create instance of myObjectvar obj = new myObject()//obj now has the proptery param1 and the function funct1//so I can do thisalert(obj.param1); //-> "property value"alert(obj.func1()); //-> "function value"

hope that helps

Link to comment
Share on other sites

All right, I'm starting to grasp it, but that also leaves me questioning why this is okay:

var r = Math.random();if (r > 0.5){document.write("Whee!");};else{document.write("Yay!");};

Why don't you have to write new Math.random? I'm sure the reason is blantantly obvious, but I'm just not getting it.Oh, and lastly... my question before, that you answered about the single/double quotes. Well, since I just discovered that you can reverse the order, is there any difference at all at any time between which one you use. What I mean is, 1 or 2:1:

document.write('<font color="blue">Hello</font>');

or 2:

document.write("<font color='blue'>Hello</font>");

I'm assuming these are always interchangeable and just want to check in case it's otherwise.

Link to comment
Share on other sites

Why don't you have to write new Math.random? I'm sure the reason is blantantly obvious, but I'm just not getting it.
random() is what is called (in some programming languages) a static method of the Math class. What this means is that you do not have to instantiate (using the "new" keyword) a new object of type Math in order to use the random() method. To confuse you even further (sorry!), I don't think you can even instantiate a new Math object.I don't think this will ever work:
var myMath = new Math();

The math class is a little bit weird in that it is a collection of methods/functions that you can call directly from the class without ever having to instantiate a new Math object:

var radius = 4;var area = Math.PI * Math.pow(radius,2);

It gets easier, I promise!As for your second question, both of the document writes will function correctly. It's just that the first one will write:

<font color="blue">Hello</font>

While the second one will write:

<font color='blue'>Hello</font>

It's a bit uglier, but, because of other programming languages I use, I tend to stick with something like this:

document.write("<font color=\"blue\">Hello</font>");

Link to comment
Share on other sites

They are called static members, it has to do with the way the class was built, for example

functon myObject(){   this.func = function()   {   }}var obj = new myObject();obj.func();

To create an object like Math you do this

var myObject ={   func: function()  {  }}var x = myObject.func();

Link to comment
Share on other sites

random() is what is called (in some programming languages) a static method of the Math class. What this means is that you do not have to instantiate (using the "new" keyword) a new object of type Math in order to use the random() method. To confuse you even further (sorry!), I don't think you can even instantiate a new Math object.I don't think this will ever work:
var myMath = new Math();

The math class is a little bit weird in that it is a collection of methods/functions that you can call directly from the class without ever having to instantiate a new Math object:

var radius = 4;var area = Math.PI * Math.pow(radius,2);

It gets easier, I promise!

I think I get it!!!! That makes a lot of sense, so thanks a bunch! Static members, eh?
Link to comment
Share on other sites

I think I get it!!!! That makes a lot of sense, so thanks a bunch! Static members, eh?
Properties and functions (also called methods) are called members of the class/object. If the are static they can be called as Object.Member without creating an new instance.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...