Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which methods are "more right"?

Status
Not open for further replies.
Dec 8, 2003
17,047
GB
After a comment that was made some time ago about using "correct" methods to do things in JS, I've been thinking about the way we code things, and their alternative methods.

While the following all work 100% (as far as I know), does anyone know of any reason why the alternatives should not be used?

1: Using "var newArr = [];" instead of "var newArr = new Array();"

2: Using "var newInt = '75' - 0" instead of "var newInt = parseInt('75', 10);"

I've been thinking that some alternatives have made it into common use so much that we rarely, if ever, use the original syntax. For example, how often do you see:

var newStr = new String();

instead of

var newStr = '';

to define a new string?

This isn't really a question I'm desperate to find an answer for, but Id be interested on hearing peoples views on the syntax and use of JavaScript, as it's pretty much my primary programming language.

Dan
 
1. AFAIK no. But I remember a slight difference between explicit and literal declaration of String object... here it is:


2. Yes, for clarity reasons. It eliminates operator precedence issues like:

var newInt = '75' - 0 * 2;

Plus string-to-number conversions happen rarely compared to anything-to-string, so maybe is better to keep them explicit.

Personally I like to write frequenty used features as short as possible... anyone knows who at W3C is responsible for document.getElementByID()? :)E
 
Good point on the operator precedence - although you could just add brackets ;o).

On the document.getElementByID() issue, a legitimate way of shorting the code would be to add this to the top of your JavaScript:

Code:
var gEBI = document.getElementByID;

And then you could simply use:

Code:
gEBI('idHere');

And you can use anything - "gEBI" is just my shortening. As far as I know, that will work in any browser on any platform.

Dan

 
Among other differences, JScript's 'reflection' capabilities will show significant differences:

Code:
BR = "<br>";

literal_string = "";
object_string  = new String();

document.write("typeof(literal_string) is " + typeof(literal_string)+BR);
document.write("typeof(object_string) is " + typeof(object_string)+BR);

document.write("literal_string instanceof String is " + (literal_string instanceof String) +BR);
document.write("object_string instanceof String is " + (object_string instanceof String) +BR);

This code produces the following output:

typeof(literal_string) is string
typeof(object_string) is object
literal_string instanceof String is false
object_string instanceof String is true

Hope that helped,

jared@eae.net -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top