September 26, 2009Simple argument defaults in JavaScript

In many other interpreted programming languages like: Python, PHP, ActionScript 3.0, etc., you can have defaults to arguments that do not get passed to functions. In the three afore mentioned languages, it's as easy as saying arg = 'default' inline in the function definition. This however is not available in the JavaScript interpreter. (It might be in ECMAScript 5, though.) But that can easily be remedied with a simple helper function.

I have met other programmers that find it ridiculous that you have to create features in JavaScript that are built into other languages. They use that to put down JavaScript and call it a terrible language. However, I think thats part of what makes JavaScript so great. JavaScript, though not as powerful in features as some other languages, offers enough flexibility and expressiveness that you can create what you feel that you are missing. Lets take a look at the problem and see how easy we can remedy it.

Read Full Article...

AddThis Social Bookmark Button Posted by Greg Ferrell at 11:52 PM. Filed under: JavaScript • (0) CommentsPermalink
April 13, 2009Execution order of functions and variables in JavaScript and ActionScript.

Many newcomers to web development may not be used to how web implementations of JavaScript execute. Namely, in what order it executes. This can be the source of many errors that are difficult to pinpoint if you aren't experienced with it. I think this also affects people who are very experienced with other programming languages and fosters a lot of undeserved hate towards ActionScript and JavaScript. (It's important to note that not all implementations of JavaScript are the same, and that some may in fact defer from what is said below. This article concerns browser and Flash based ECMAScript.)

The thing to understand about JavaScript is that it isn't compiled for the most part. (This is not exactly true for ActionScript because of the introduction of classes in ActionScript 2.0, however, the runtime script remains the same. You might argue that it compiles into a *.swf file, but in reality, that is the combination of everything that has to do with the Flash file. The ActionScript still remains live.) It's good that it isn't compiled though, because this allows for much more dynamic, expressive coding.

Read Full Article...

AddThis Social Bookmark Button Posted by Greg Ferrell at 03:17 AM. Filed under: JavaScript • (0) CommentsPermalink
January 10, 2009Type-checking arrays in JavaScript

In JavaScript the typeof operator reports an array as 'object'. While this is technically correct (an array in JavaScript is really just a specialized object), it's a pain in the neck when you are type checking a variable.

There are a number of ways to identify an array in JavaScript. Checking for properties of an array isn't totally accurate because you can add any named member to an object to imitate an array. Checking for 'instanceof Array' works most of the time but could be tricky in some implementations of JavaScript.

Read Full Article...

AddThis Social Bookmark Button Posted by Greg Ferrell at 04:06 AM. Filed under: JavaScript • (0) CommentsPermalink
December 29, 2008JavaScript Errors: Testing for the existence of functions in JavaScript

When using multiple JavaScript libraries, it is inevitable that one JavaScript file will need to call functions named in other libraries. This is necessary but can cause issues if all of the libraries aren't present.

Error handling with JavaScript is somewhat lackluster. With this in mind, you should do your best to catch and handle errors and avoid sending them to the end-user. In order to prevent errors and allow for scalability, you should test for functions that aren't present in the script where you are calling them.

Read Full Article...

AddThis Social Bookmark Button Posted by Greg Ferrell at 06:38 PM. Filed under: JavaScript • (0) CommentsPermalink
January 10, 2008Parsing a nested or name spaced object from a string in ECMAScript

Often times when I am making dynamic scripts in Flash or JavaScript, I want to call an object method using a string. It may be a situation where I am stuck using fscommand (due to a customer request of a Flash version <8) or just writing script with script.

The function is executed somewhat simply by splitting the string at the dots and adding them as subscripts to one another in order. However, there are some other pieces to it that make it a little more friendly to mistakes and general usage.

Read Full Article...

AddThis Social Bookmark Button Posted by Greg Ferrell at 02:37 AM. Filed under: JavaScript • (1) CommentsPermalink

Page 1 of 1 pages