News for hackers. Stuff that matters.
Re:A re-introduction to JavaScript by ams6110 on Saturday July 17, 2010 @08:01PM (#1524904)
Glad to see they linked Crockford's site. I discovered his site probably four years ago or so and it really opened my eyes to the capabilities of the language. I think anyone who wants to learn more about JavaScript should definitely include Crockford in his studies.
Re:A re-introduction to JavaScript by aborovoy on Saturday July 17, 2010 @04:01PM (#1524531)
Question to JavaScript gurus: how would i know if my code works in various browserless environments like Reeno and V8? Is there a compatibility table of various JS features is isn't browser-centric?

Thanks!

Re:A re-introduction to JavaScript by geuis on Saturday July 17, 2010 @04:40PM (#1524594)
Don't down vote this fellow's question, its perfectly valid and interesting.

First, you have to make a distinction between Javascript(ECMAScript) the language, and the API's that an environment makes available to it.

A good example of this is document.getElementById(). This is universally found in browsers, but you won't find this in Node.js (a server-side javascript environment using V8). Conversely, Node makes available Buffers which you won't find in any browser.

I think the best way to answer your question is to identify in the respective environments which version of ECMAScript they claim to support. Various versions of each browser support different versions of ECMAScript, or such as Internet Explorer where earlier versions supported a variant called JScript that didn't always tie directly to the published standards.

The wikipedia page is a good place to start, http://en.wikipedia.org/wiki/ECMAScript.

Re:A re-introduction to JavaScript by tlrobinson on Saturday July 17, 2010 @06:10PM (#1524758)
Get "JavaScript: The Definitive Guide" and look at the API reference, it's split into two sections, one for APIs provided with every JavaScript implemetation, and one for APIs provided by browsers.

Currently there's not much compatibility between server-side JavaScript platforms (beyond the minimal set of APIs provided by the language implementations), something we're trying to solve with CommonJS, but that effort has been inhibited by node.js's sudden rise in popularity and refusal to work with CommonJS...

Re:A re-introduction to JavaScript by micrypt on Saturday July 17, 2010 @06:56PM (#1524823)
"refusal to work with CommonJS..." I thought this was a thing of past. Node.js is listed on the CommonJS website: http://www.commonjs.org/impl/
Re:A re-introduction to JavaScript by tlrobinson on Saturday July 17, 2010 @08:28PM (#1524933)
Node has CommonJS modules, which is a good start, but that's it.
Re:A re-introduction to JavaScript by pdelgallego on Saturday July 17, 2010 @06:13PM (#1524764)
The best option is to avoid everything that is not part ECMAScript 262, and even programming according to the spec you maybe at some point some inconsistencies.

Check out the sputnik test project[1] to see what is working in the different VM.

An easy example of inconsistency : If you read the specs you will find that FunctionDeclarations are only allowed to appear in Program or FunctionBody. Syntactically, they can not appear in Block s. But because the specs allowed extensions you can find that Mozilla/rhino/spidermonkey implement a different way that Webkit/V8

run this code in firebug and in the inspector a you will see that produce two difference.

<code><pre> if (true) { function foo(){ return 1; } } else { function foo(){ return 2; } } foo(); </pre><code>

Indeed things are not that bad, because is easy to avoid it.

var foo; if (true) { foo = function foo(){ return 1; }; } else { foo = function foo() { return 2; }; } [1]sputnik.googlelabs.com/

Re:A re-introduction to JavaScript by pdelgallego on Sunday July 18, 2010 @04:33AM (#1525435)
I mean

  === 
   if (true) { 
    function foo(){ return 1; } 
   } else { 
     function foo(){ return 2; } 
   } foo(); 



  === 
   if (true) { 
     foo = function (){ return 1; } 
   } else { 
     foo = function (){ return 2; } 
   } foo();
Re:A re-introduction to JavaScript by zwetan on Sunday July 18, 2010 @02:25PM (#1526348)
tests for those environments

Sure sticking to ECMA-262 can help http://www.ecma-international.org/publications/standards/Ecm...

you can read, re-read, etc. the spec to get a good feel of the language but at the end of the day, some hosts could implement things a bit differently, especially on the "blurry" parts of the spec.

For example, two engines could support regexp but have a slightly different implementation and be both valid ECMA-262 edition 3, idem for number precisions, etc.

So if your main concern is to avoid browser-centric JS features, test with the engine of those browsers http://en.wikipedia.org/wiki/JavaScript_engine * Chrome => V8 * Firefox => SpiderMonkey / TraceMonkey * Internet Explorer => IE JScript / Chakra * Safari => JavaScriptCore / SquirrelFish * etc.

most of the browsers will provide their engine as a command line tool ex: https://developer.mozilla.org/en/Building_only_SpiderMonkey (for IE you can use Windows Script Host).

and simply put, you can run your JS code base on those tools to find the common denominator and/or limitations, imho it is simpler to run tests on the command line than in the browser itself.

If already your library work with all the engines you're in pretty good shape.

Another way to be in "good shape" is to simply re-use one of the popular JS libraries, like JQuery, as those are pretty well tested.

But to be honest it depends on how far you want to go, most likely testing only the JS engine will not be enough if you deal a lot with the browser DOM, you will have to test in the browsers anyway, and there that's where the fun begin, because as much as you can find slight difference of implementations in JS engine, you can find bigger differences in browser implementations, yep even if all that is supposed to be "standard".

here few links that may help

http://www.quirksmode.org/js/support.html http://www.quirksmode.org/compatibility.html http://www.quirksmode.org/js/introevents.html http://ejohn.org/blog/building-a-javascript-library/ http://code.google.com/speed/articles/optimizing-javascript....

Re:A re-introduction to JavaScript by csallen on Saturday July 17, 2010 @04:02PM (#1524532)
Just a heads up, article was last significantly edited in March '06.
Re:A re-introduction to JavaScript by tlrobinson on Saturday July 17, 2010 @06:04PM (#1524748)
Fortunately (unfortunately?) the language hasn't significantly changed either.
Re:A re-introduction to JavaScript by sloniks on Saturday July 17, 2010 @06:20PM (#1524782)
What has changed since then?
Re:A re-introduction to JavaScript by jackmoore on Sunday July 18, 2010 @03:31PM (#1526520)
My only qualm is that it gives advice on how to use arguments.callee, which was depreciated in ECMAScript 5, for recursive anonymous functions.

Since they already give an example of how to use a named function recursively, the arguments.callee example could just simply be removed.

Re:A re-introduction to JavaScript by banuelios on Saturday July 17, 2010 @05:24PM (#1524670)
Look a few lines down:

  Page last modified 10:04, 30 Jun 2010 by user01
Re:A re-introduction to JavaScript by edd on Saturday July 17, 2010 @05:48PM (#1524711)
No really it was last significantly edited in 2006. Look at the contents of the revisions for the past 3 1/2 years: https://developer.mozilla.org/index.php?title=en/JavaScript/...
Re:A re-introduction to JavaScript by AlexMuir on Saturday July 17, 2010 @04:02PM (#1524535)
I've been using Javascript for years and never really 'learned it'. It's just something I've dipped into to solve a particular problem - largely through just grabbing other code/dissecting existing stuff. Then I got hold of jQuery and never learned about the underlying language. This was great to catch up on the basics.
Re:A re-introduction to JavaScript by andrew-bor on Saturday July 17, 2010 @03:57PM (#1524520)
Despite hundreds of thousands of search results in Google when you're looking for "Javascript tutorial" this one is by far the best one for a beginner.
Re:A re-introduction to JavaScript by commandar on Sunday July 18, 2010 @01:52AM (#1525319)
Provided you have some familiarity with C like languages. I'm skimming through this now, and it really is quite well-written, but I'd be totally lost if I didn't have some experience with C/C++ in school years ago since it does make a number of assumptions that you'll just 'get it' -- e.g., the section on for loops pretty much says "they work like in C." :)
Re:A re-introduction to JavaScript by jckarter on Saturday July 17, 2010 @04:26PM (#1524575)
An introduction really shouldn't encourage things like "for (var i = 0, item; item = a[i]; i++)". Someone's going to cut-and-paste that because it looks cute, and their code will break when their array suddenly needs to contain null values. Fuzzy boolean semantics are bad enough without spreading stuff like this around (speaking from experience with || abuse in Perl).
Re:A re-introduction to JavaScript by freakwit on Saturday July 17, 2010 @04:44PM (#1524604)
To be fair, they do note this shortcoming.

"Note that this trick should only be used for arrays which you know do not contain "falsy" values (arrays of objects or DOM nodes for example). If you are iterating over numeric data that might include a 0 or string data that might include the empty string you should use the i, j idiom instead."

Re:A re-introduction to JavaScript by WilliamLP on Saturday July 17, 2010 @06:43PM (#1524813)
So it should be used approximately never in real code.
Re:A re-introduction to JavaScript by jackmoore on Sunday July 18, 2010 @04:45PM (#1526679)
I also think it should discourage modifying built-in objects. Unlike their for loop, the following line comes with no warning or disclaimer: "Interestingly, you can also add things to the prototype of built-in JavaScript objects."
Re:A re-introduction to JavaScript by jackmoore on Sunday July 18, 2010 @03:13PM (#1526476)
Agreed. But what is the point of declaring the item variable? Wouldn't "for (var i = 0; a[i]; i++)" mean the same thing, but more concise?
Re:A re-introduction to JavaScript by llaxsll on Saturday July 17, 2010 @04:14PM (#1524550)
This article has a good description of closures, which is very important to brush up on if one is developing a heavy dhtml site. Thanks.
Re:A re-introduction to JavaScript by AlexMuir on Saturday July 17, 2010 @04:20PM (#1524560)
DHTML - That's a blast from the past. Don't see that acronym any more.
Re:A re-introduction to JavaScript by llaxsll on Saturday July 17, 2010 @04:29PM (#1524581)
What would you call it, Mr. AlexMuir ? :)
Re:A re-introduction to JavaScript by AlexMuir on Saturday July 17, 2010 @06:16PM (#1524771)
I'm not saying it's the wrong term, just that I've not seen it for ages. I think people just sort of take it for granted nowadays that HTML probably includes Javascript, rendering the D for dynamic a bit redundant.
Re:A re-introduction to JavaScript by jasonjohnson on Saturday July 17, 2010 @05:25PM (#1524671)
Not a bad guide, old as it may be. Chuckled a bit when I read, "JavaScript has a tertiary operator for one-line conditional statements..." - been in there since the original version.

http://en.wikipedia.org/wiki/Ternary_operation

Re:A re-introduction to JavaScript by WilliamLP on Saturday July 17, 2010 @06:41PM (#1524808)
That could be worked into a great programming language misappropriation-based comedy routine! Also the one about the guy who said "overriding" when he meant "overloading" is usually good for a laugh.
Re:A re-introduction to JavaScript by limist on Sunday July 18, 2010 @07:39PM (#1527084)
If you like this article, you'll love the book "Javascript: The Good Parts":

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockfor...

Re:A re-introduction to JavaScript by michaeltwofish on Saturday July 17, 2010 @10:32PM (#1525107)
This article provided my "aha!" moment for prototypes.