tags:

views:

402

answers:

11

I'm looking to do a presentation in a couple of weeks and was wondering: What are the top ten Javascript incompatibilities that you look out for when developing? What trips you up? I can start with one:

var somevar = {
 'internet': 'explorer',
 'hates': 'trailing',
 'commas': 'in',
 'json': 'code', // oh noes!
}

What are some other common gotchas that can't or aren't fixed by using a framework like jQuery or base?

A: 
$('asdf')
    .a()
    .b()        // Woops!  Forgot a semicolon.

$('qwer').d();
strager
What would this do here? If JS encounters something untoward after a line break, it adds an implied semicolon doesn't it? Is this one of those gotchas where that isn't the case or something?
thomasrutter
Could be related to multiline statements, may well stop the implied statement termination.
meandmycode
I tried it out and couldn't find a difference when the whitespace was a new line. When the whitespace was just a space, both browsers complained. I can't see the inconsistency either.
altCognito
+7  A: 

With HTML markup like

<div id="foo">
    <a href="#">Link 1</a>
</div>

If you obtain a reference to the outer div, it will have one child node in some browsers, and three child nodes in others, depending on how whitespace is treated. Some will have text nodes with the newline and whitespace as children of div#foo before and after the link element.

levik
fortunately, this is something that libraries like prototype.js take care of.
Thilo
I'm not sure that any library can handle all situations though as parsing of whitespace into the DOM isn't consistent across browsers, which I think was the point. (there is a loss of data which is impossible to replace) I'll try it later in some browsers to verify.
altCognito
More of a DOM issue than a JS one though, JS is quite consistent about walking through the DOM
annakata
A: 

There are almost no inconsistencies in JavaScript implementations across browsers. If there were, it would be a nightmare multiplied (by the multiple inconsistencies in DOM implementation). So "Top Ten Javascript Incompatibilities/Inconsistencies" can probably only be populated with "Bottom One" you spotted. Indeed there could be a pair more, but I would hardly expect them to be major or even worth attention.

Sergey Ilinsky
A: 

DOM API inconsitencies aside (which is what libraries solve), there aren't many.

But some JS engines have implemented more features, like [].map(), [].filter() etc, let statement or E4X. When I switch between Mozilla-targeted development (Firefox extensions, server-side with Jaxer) and general browser tageted development (websites) I need to remember what's available in every browser, and what isn't. So it's all about what is implemented, not how it is done.

pawel
+5  A: 

Well, there's a problem with implied semicolons.

return {
    a: 1,
    b: 2
}

Some people like opening brackets on its own line, like this:

return
{
    a: 1,
    b: 2
}

However, this last statement will return undefined, since the parser sees:

return;
{
    a: 1,
    b: 2
}
Magnar
Thats a really good one.
meandmycode
But is it an incompatibility?
reinierpost
No, it's more of a gotcha. There aren't many incompatibilities in JS any more. The DOM is the culprit.
Magnar
+3  A: 

var x = new Boolean(false); if (x) ... else...;

Is the 'if' or 'else' branch taken?

var x = "hi", y = new String("hi");

what is the typeof(x) and typeof(y) ?

Edit:..

parseInt("017") produces 15 (octal) instead of 17

The 'Error' object is a different signature from IE to Firefox.

When using objects as hashmap's, you need to use object.hasOwnProperty(key) to ensure that the property is not inherited through the prototype chain.

Mathew
So where are the Javascript incompatibilities?
Sergey Ilinsky
Whle not exclusively inconsistencies between browsers, this answer provides the most inconsistencies in the language.
altCognito
+2  A: 

Another one (don't think this comes up very often):

(typeof document.getElementById)

in IE: "object"

in Firefox: "function"

altCognito
A: 

Yet another one:

Regular expressions from Firefox to IE, there are lots of inconsistencies:

For the description: http://blog.stevenlevithan.com/archives/cross-browser-split

He publishes a fixed version of the RegEx object, and a testing page: http://stevenlevithan.com/demo/split.cfm

altCognito
A: 

For the Date object:

alert( (new Date()).getYear();

Firefox returns 109

Internet Explorer returns 2009

altCognito
A: 
alert(document instanceof Document)

in Firefox: true!

in Internet Explorer: exception: Document is undefined

altCognito
A: 

An extension to altCognito's point.. Element isn't a defined base type either...

//uses jQuery
function getSomething(input) {
   if (typeof(input) == string)
       input = $(input)[0] || $('#'+input)[0] || null;

   if (input instanceof Element)
       input = $(input);

   if (input instanceOf jQuery) {
       ...do something...
   }
}

I had to replace the instanceOf Element with..

if (input && input.tagName)...

Would be nice if DOM elements were properly base classed in IE all around.

Tracker1