Jump to content

Error In Javascript Quiz


jexus

Recommended Posts

Question 18 of the JavaScript states that the "correct" way to open a new window is "window.open". This is not true, "open" is the correct way!"window" represents the global object, so there is no actual need to specify it when accessing a global variable. Usually, the use of "window" signifies a lack of understanding of the nature of language, especially in the browser.Another question asks the correct way to instantiate an array. The use of new Array() is pretty cruel to people like me who use arrays more often than not. [] is the best way to write an array, just like {} is the best way to write an object.No hard feelings though, w3schools.com is and will always be a great resource! :-)

Link to comment
Share on other sites

JavaScript and especially the HTML DOM are extremely object oriented. There are very few functions that exist independent of native objects or DOM objects. open() is technically a method belonging to the window object. Since window methods are always executed within the scope of a window object, they can be called without referencing the window itself.I agree the window.open() question is misleading. But I wouldn't exactly call it in error.Arrays. Hmm. The "new Object" technique coexists happily with shorthand versions that do the same thing. To say that one is cruel or better does not address the issue of correctness. It doesn't even make sense, really. Best for you may not be best for me, or best for me in a certain context. As PERL programmers are fond of saying, "There is more than one way to do it." In any case, the JSON/literal techniques that you mention may be correct, but they are not options on the quiz. As in any test, the taker's job is to select the best response, and in this case, only one actually works.The question could be more expansive, I guess, but at the expense of clarity.FWIW, I'm of the school that says we should learn things the long way first, because the long way usually helps you to understand the concepts better. Shortcuts can come later. That's my $.02

Link to comment
Share on other sites

JavaScript and especially the HTML DOM are extremely object oriented. There are very few functions that exist independent of native objects or DOM objects. open() is technically a method belonging to the window object. Since window methods are always executed within the scope of a window object, they can be called without referencing the window itself.I agree the window.open() question is misleading. But I wouldn't exactly call it in error.Arrays. Hmm. The "new Object" technique coexists happily with shorthand versions that do the same thing. To say that one is cruel or better does not address the issue of correctness. It doesn't even make sense, really. Best for you may not be best for me, or best for me in a certain context. As PERL programmers are fond of saying, "There is more than one way to do it." In any case, the JSON/literal techniques that you mention may be correct, but they are not options on the quiz. As in any test, the taker's job is to select the best response, and in this case, only one actually works.The question could be more expansive, I guess, but at the expense of clarity.FWIW, I'm of the school that says we should learn things the long way first, because the long way usually helps you to understand the concepts better. Shortcuts can come later. That's my $.02
Not exactly true, window *is* the global object. I don't exactly see what OO has to do with this, as JavaScript is a prototype-based language, not class-based. OO is the wrong term for this language. Of course, just about everything is an object, but that's not what makes a language Object Oriented.Anyway, the point of my post isn't exactly calling it an error, but it is wrong to say that "open" on it's own is not correct, because it is *more* correct than window.open. Why write window.open but use document on it's own? It should be window.document then, as it's also a property of the window object. And that would make alert window.alert, though the right answer for that question doesn't mention window a single time.And of course, you should always learn the long way first, I also learned it the long way but now I wished I immediately knew about shorthand notations. {} also has a performance win over "new Object()", since it is optimized, while new Object() is treated like normal code :)
Link to comment
Share on other sites

JavaScript is OO - prototype-based languages can be (and more often than not are) OO, and JS fulfills the characteristics of an OO language - i.e. objects can be extended, members can be encapsulated within objects, and objects can be used in different manners without definition modification. Even Wikipedia thinks it's object-oriented :)However, I do agree, it is no more "right" to use window.open() than it is to use open(). The array instantiation one sounds very "school-like" to me, though, it is more formal to write new Array() than it is to use JSON notation.Don't know about the "JSON is faster" thing - benchmark anyone?

Link to comment
Share on other sites

Don't know about the "JSON is faster" thing - benchmark anyone?
I ran the following code on my Mac (2.4GHz). Two tests are available. Just bloop out one or the other. (Yes, it could be more elegant, but it gets the job done.)
function trial () {	var d1 = new Date();	for (var i = 0; i < 100000; ++i) {		var arr = new Array ("apple", "frog", "semester", "trio", "bootjack", "leopard", "earthquake", "computer", "liquifaction", "addressee");	//	var arr = ["apple", "frog", "semester", "trio", "bootjack", "leopard", "earthquake", "computer", "liquifaction", "addressee"];		delete arr;	}	var d2 = new Date();	var elapsed = (d2.getTime() - d1.getTime()) + " milliseconds";	document.getElementById("results").innerHTML = elapsed;}

Basically, I created and destroyed 100,000 arrays in each run. I ran about a dozen trials each time. I tested the 2 most popular Mac browsers. Each browser was the latest update.On Firefox, each method hovered around 28 ms. Very little variation, with no noticeable difference between the array constructor technique and the JSON technique. Yes, I could run 100,000 trials of 100,000 trials and do the math and maybe there would be a difference you could notice. But why bother?This part is interesting. I ran the same trials on Safari. The JSON technique took roughly 100 ms longer (just under 400%) than it did on Firefox. So the variation between browsers was significantly greater than the variation between Firefox techniques. BUT: the variation on Safari was also large. The times running the JSON technique hovered around 130ms. The times running the constructor technique hovered around 98 ms.To sum it up: On Firefox, there was no significant difference between techniques. On Safari there was a 32% difference between the techniques, the constructor method being the faster.Even so, the difference between browsers (400%) was greater still.FWIW, I get the same results when the array is nullified (instead of deleted and created again).I'm curious to see results on Windows, including IE7-8. (I'm at work now, and my Windows machine is home.)

Link to comment
Share on other sites

Results for Windows:

  • Firefox 3.5: Roughly the same for both methods: around 30ms.
  • IE7 and IE8: Normal method takes approximately 450ms, while JSON takes 420ms.
  • Opera 10: Normal method takes around 375ms, JSON takes around 200.
  • Google Chrome: Normal method takes approximately 30ms, JSON takes 15ms.
  • Safari: Normal method takes approximately 125ms, JSON takes around 160ms

The observations conclude that JSON is faster in all browsers except Firefox and Safari. In Firefox it's equal, while in Safari it's slower.I still don't see it as a mistake to teach the normal method of creating arrays and objects.

Link to comment
Share on other sites

Anyway, the point of my post isn't exactly calling it an error, but it is wrong to say that "open" on it's own is not correct, because it is *more* correct than window.open.
It's true that it's wrong to mark open as incorrect, but I don't agree that open is more correct than window.open. There is an inconsistency, sure, so for the same reason it would also be more correct to teach window.alert, window.document, etc, and also make sure that people know that the default namespace is the window object so that it's not necessary to qualify the properties and methods of it. I think that if you just told someone to use open, alert, document etc without even mentioning the window object then that's probably the worst way to teach, they at least need to know that they're using the window object but don't need to call it by name. If they understand that concept they are more likely to understand the nature of JS in general. Anything else just leaves out information that may be important to the learning process. Likewise with the window object, students should be taught both ways to instantiate an array and object, leaving one of them out just makes it incomplete.Also, that's weird about Safari, clearly it's does things differently than other browsers. I guess that's the whole "think different" mentality, eh? I'm sure Steve Jobs is out there in a turtleneck somewhere telling a room of people nodding their heads why the constructor method is a groundbreaking innovation.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...