functions posts

Javascript: Closures, Scope, and Arrays

Closures are used quite frequently in Javascript for tasks such as adding event listeners or setting timeouts on function calls. Closures are where a function is passed as a parameter to a function call from another function, and variables from the calling function must be used inside the parameter function. Dealing with scope in closures can be difficult, and I’ve spent a lot of time figuring issues with them out.

An early issue I ran into with scope, and a common one, is the loss of scope of the “this” keyword in the closed function. For example, you might want to do a setInterval that references the object that created it. To do so, you can simply create a variable pointing to “this” and then use that in the closed function, like:

class.prototype.thefunction = function(){
    var fncThis = this;
    setInterval(function(){ fncThis.doSomething(); }, 1000);

This is also a common problem with event listeners, where “this” might be hoped to point to the element the listener is related to, but doesn’t.

Recently, I ran into a closure problem while revamping the menu script we use at Cogneato for suckerfish menus from the old MM functions to something more capable.

Continue reading post "Javascript: Closures, Scope, and Arrays"

Formatting Dates in Classic ASP

During my internship at RPM, we used classic ASP for server side scripting rather than the PHP I’m more used to. Classic ASP is missing functions for formatting dates, like PHP and *NIX shell have, for instance. SQL Server has the CONVERT function, but only has a limited number of output formats, at least for the older SQL Server version we had: Otherwise, it would be more efficient to format the data as it is coming from the database . I built two functions for date formatting based on the PHP and *NIX “date” formats for use on the HSGA site, where I had to format dates a certain way for a project. I don’t remember if I used it on other projects, but I think so.

Both functions take two parameters. The first is a date, as would come from a SQL Server “datetime” field. The second is a string that defines the output format. The first function uses a format string like the php date function. The long function is as follows (tabs are double spaces due to the width and length of the content):

Continue reading post "Formatting Dates in Classic ASP"

PHP Functions: Array as Argument

A while back, I wrote about using the JSONesque literal value parameters in Javascript, like jQuery does. This allows arguments to be passed: with names, in no particular order, all being optional. I set it up so that multiple arguments could be used as well, allowing for existing functions to still work or people who prefer that syntax to have it. I will now write about something similar for PHP.

In PHP, it is not quite as elegant, but almost. An array with key-value pairs is passed as the single argument for named argument mode. So you could call like this:


or with regular arguments:

Continue reading post "PHP Functions: Array as Argument"

Javascript: Literal value function parameters

I’ve been messing around with Javascript a bit lately, creating an AJAX object just now, doing stuff for school as well. One of my classes is very much focused on Javascript. I’ve noticed JQuery and some other examples making use of a single JSONesque (javascript literal values, of which JSON is a subset) parameter for functions to allow many parameters to be entered. They are all potentially optional and can be in no particular order. Seems wonderful, especially for Javascript, where there’s no overloading of functions. This is sort of what I thought Objective C would allow, since it requires naming of all variables. Unfortunately, objective C requires them to be named as well as in the exact order, and none can be missing unless they’re optional and at the end, so it is really just a pain there. But in Javascript, this method does indeed allow what I want. So you do have to know the exact parameter names, but don’t have to remember order, and can just as easily not put in parameters if the function will operate without.

It’s rather simple to handle. I was even able to modify an already in use function to use one literal value parameter or the many that it originally used, simply by checking the arguments.length function property to see if it is one, and populating the appropriate variables if so. So to handle the properties, they are appended to the argument and accessed by the key in dot notation. Example function:

function ExampleFunction(argJson){
    document.write("Key1 = " + argJson.key1);
    document.write("Key2 = " + argJson.key2);
Continue reading post "Javascript: Literal value function parameters"