PDA

View Full Version : General Javascript questions



Eclyps19
10-01-2006, 05:13 PM
Okay. So i've been reading up, and I am going to be coming here with my random questions. Instead of making a new thread that can be answered in one post for every question i come across, I'm going to start posting them all in here.

Question #1 - Arrays

so you can make an array by using the following syntax

var a = new Array();
a[0] = 1;
a[1] = 2;
a[2] = 3;
and so on.

First of all, if you try to find the lenth of the array, what will it tell you? Since you didn't define the length when you created the array, does it even have a length?

What if you try assigning something to like the 400th element in the array without assigning something to the other 399 elements? Will it go ahead and let you do that?

Thanks.

blm126
10-01-2006, 06:24 PM
First of all, if you try to find the lenth of the array, what will it tell you? Since you didn't define the length when you created the array, does it even have a length?

Yes it has a length. You can find it with the length property of the array. This property will update as you add more keys. Please not that the returned length starts at 1 while the keys start at 0.


What if you try assigning something to like the 400th element in the array without assigning something to the other 399 elements? Will it go ahead and let you do that?

Yes

Eclyps19
10-01-2006, 08:05 PM
so if i do a loop to display all elements from 0 - 400, it would display undefined up until 400?

Twey
10-01-2006, 09:37 PM
It would output:

1
2
3
undefined
undefined
undefined
undefined
undefined

... and so on.

Another thing to note is that .length is the highest index in the array plus one. So:
var a = [];
a[0] = "a";
a[13] = "m";a.length is 14, despite the fact that 1 through 12 are undefined.

Eclyps19
10-01-2006, 09:55 PM
ahhh i get it. thanks for clearing that up. More questions to come.

Eclyps19
10-02-2006, 11:38 AM
Question #2 - Variable scopes

Say you are making a for loop.


for(i = 0; i < 10; ++i)
{
documment.write(i);
}


this will work just fine, but what is the scope of the variable i? Since I didn't declare it using var, is it limited to the for loop? or can it be accessed outside of the loop?

Eclyps19
10-02-2006, 02:16 PM
Question #3 - this. and document.

So when are each of these used?

How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?

Twey
10-02-2006, 04:56 PM
what is the scope of the variable i?Since you didn't use var, it is global unless it has been defined elsewhere.

A note on scope in Javascript: I know you are proficient with Java, and one common mistake people often make when moving to Javascript from Java and similar C-like languages is to assume that a code block denotes a new scope. This is not so in Javascript.
Question #3 - this. and document.document is the DOM reference to the root of the document (the <html> element). There's little that can be done directly with document, but it contains many properties and methods that are handy for altering the content of the page, such as body, forms[], images[], getElementById(), getElementsByTagName(), and the like.

this, on the other hand, refers to the object containing a method. Inside document.getElementById(), for example (if it were written in Javascript, that is) this would refer to document. If a method doesn't belong to an object, this refers to the global object (window in the case of browsers). Event handlers are properties of their respective elements, so inside an event handler, this usually refers to the element that caught the event.
How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?The property I assume you mean, document.body.bgColor, is deprecated in favour of the CSS-compliant document.body.style.backgroundColor.

mburt
10-02-2006, 07:21 PM
How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?
Document refers to the root of the document (DOM). "this." refers to the element that the event handler is coming from (event.srcElement).

Eclyps19
10-07-2006, 12:56 PM
Question #4 - Adding .js files.

So I made a .js file to find imaginary numbers using a C++ program I made as a reference. I'm pretty sure it all works. Only problem is I am not really sure how to add it to the main page. I need something like an #include but for javascript.

And when I want to test it out, I can use it just like any other function, right?

Twey
10-07-2006, 02:39 PM
<script type="text/javascript" src="imaginary_numbers.js"></script>Yes.

Eclyps19
10-16-2006, 12:32 PM
Question #5 - What's the difference?

What is the difference between
<script language="javascript">
and
<script type="text/javascript">?

codeexploiter
10-16-2006, 12:45 PM
type attribute

This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.

language attribute

This attribute has already been deprecated. This attribute specifies the scripting language of the contents of this element. Its value is an identifier for the language, but since these identifiers are not standard, this attribute has been deprecated in favor of type.

Eclyps19
10-16-2006, 12:59 PM
so what is the 'norm' for programming in javascript? Are they really that different? It sounds like they are pretty much the same thing, but one requires slightly more typing.

Can you give me an example of what I'd use each one for? Not actual code, but just describe something. Thanks

mwinter
10-16-2006, 03:59 PM
document is the DOM reference to the root of the document (the <html> element).

The root of the document and the root element are not the same thing. The document object is the document itself. The documentElement property of the document object is a reference to the root element.



this, on the other hand, refers to the object containing a method.

That's not a very meaningful description in a language that has objects that can be modified at will. Moreover, it doesn't really describe the behaviour of the this operator.

For the first point, consider:



function myFunction() {}

var myObject = { method: myFunction };

Which object "contains" the function object? Which object does it "belong" to? The global object or the object referenced by myObject?

The value of the this operator is determined by use, and only that. If a function object is called as part of a member expression (dot or bracket notation), then the operator refers to the penultimate object in that expression. That is, in



a.b.c();

the this operator will refer to the object referenced by the b property. I've written about this many times in the comp.lang.javascript newsgroup, as have others. One can search the Google Groups archives for more information.



Event handlers are properties of their respective elements, so inside an event handler, this usually refers to the element that caught the event.

I would consider the "element that caught the event" to be the target of the event, which isn't necessarily the same thing. Perhaps another way of expressing it is to say that the this operator refers to the element to which the event listener is attached:



<p onclick="alert(this.tagName);">Some <span>meaningless</span> text.</p>

If one clicked on the word "meaningless" it would be the event target (referred to by the target or srcElement properties of the event object), but the dialogue box would display "P" as the listener is attached to that object.

Note that if the attachEvent method is used in MSIE to add event listeners, the this operator will refer to the global object. It's for this reason that I typically avoid using it.




"this." refers to the element that the event handler is coming from (event.srcElement).

As I wrote above, the srcElement (or target) property refers to event target. The this operator refers to the node that is currently executing its listeners (the currentTarget property in the W3C DOM).




so what is the 'norm' for programming in javascript?

Are you still referring to attributes of the script element? If so, the answer, for any decent markup, would be the type attribute.

Mike

Twey
10-16-2006, 04:23 PM
Can you give me an example of what I'd use each one for? Not actual code, but just describe something. Thankslanguage wouldn't be used for anything. It's deprecated.