a simple Regular expression explained

May 19, 2011 0 Comments
Tagged: and

This is just for me, so please ignore this...

I needed a regular expression and as I am NOT good at them, I dissected this one I got from @nickfitz and extended it a tad.

// the expression
^([1-9]\d)+,?\d{0,2}

// start of the line
^
(
// a numerical character 1 to 9 ( no 0)
[1-9]
// any numerical char
\d
)
// the stuff between brackets above this can be repeated. It should be
there one time or more (another possibility would be *, that would
require zero times or more repeating of the above)
+
// an optional comma
,?
// some numbers can be none up to two
\d{0,2}

This should match:
1
15
15,
15,5
15,66

but not:
01
15.
15.55
15,789

Besides the help on twitter from jlix and tijn I used reggy to achieve greatness :).

Different var, same subject

Apr 28, 2011 1 Comment
Tagged: , and

I recently tried to do something and I don't know if it is all that good. If somebody has an opinion about it, I would love to hear it.

I have been using the $ suffix for variables that are really jQuery objects. But I keep coming across occurrences that need the real variable, aka not the jQuery one.

So what I tend to do now is something like:

collection : {
$foo : $('.bar'),
foo : '.bar'
}

In this way I can use both the jQuery object easily and the normal variable, like this:

// use the jQuery object
collection.$foo.action()// use the normal object
$('body').find(collection.foo)

What do you think, is this too much or do you use this too?

I am trying to get a better last example, 'cause this ain't all that clear I think...

JavaScript disabled?

Oct 13, 2010 0 Comments
Tagged: and

nicholas zakasI just came across a small article by Nicholas C. Zakas (@slicknet), who I saw give a great presentation at last fronteers conference by the way, in which he states:

"While the percentage of visitors with JavaScript disabled seems like a low number, keep in mind that small percentages of big numbers are also big numbers."

(Via YDN Blog.)

Please remember that the same thing goes for visitors who are:

  • Using IE6
  • a screen reader.
  • a mobile phone
  • a non iphone phone, like symbian
  • ...

which one is faster?

Sep 28, 2010 4 Comments
Tagged: and

People, is there anybody who can help me. I am wondering which one is faster...

Calling this several times in a script:

var foo = $(data).find('foo').text();
var bar = $(data).find('bar').text();

Or wrapping it in a function:

var findData = function (data, field) {
var v = $(data).find( field ).text();
return v;
};

And calling that as many times:

var foo = findData(data,'foo');
var bar = findData(data,'bar');

I know that the last solution appeals to me more, maybe 'cause it looks cleaner. But what I am wondering, which is faster?

After reading the comments, I think I'll go for:

var $data = $(data);
var foo = $data.find('foo').text();
var bar = $data.find('bar').text();

Tip of the hat to Codepo8 / Chris

Javascript coding suggestions

crockford I am now working on some coding conventions for use at one of my clients. Having done some work on the HTML and CSS ones, I recently started with the javascript one. As I was working on it, I looked at the work done by others, like Douglas Crockford. Some of my co-workers suggested that we did this:

var nameSpace = (function () {
// declare all of your var's before using them..
var config, init,x,doStuff;
config = {
a : 'a'
};
init = function ( arg ) {
doStuff( arg );
};
x = '36';
doStuff = function ( arg ) {
// dostuff
};
return {
init:init
};
}());

While I initially agreed with this, as it seems nice and neat, I spotted a problem with it when it gets big. As you get a really big script and more people working on it, chances are that you will get someone to forget to declare a var in the beginning of the function, hence making it global.
Like this:

var nameSpace = (function () {
// declare all of your var's before using them..
var config, init,x,doStuff;// we forgot to add doMoreStuff
config = {
a : 'a'
};
init = function ( arg ) {
doStuff( arg );
};
x = '36';
doStuff = function ( arg ) {
// dostuff
doMoreStuff();
};// doMoreStuff is global
doMoreStuff = function () {
alert( config.b );
};
return {
init:init
};
}());

So I came up with a (not so revolutionary) way of avoiding this and I am curious what you think of it?
It goes like this:

var nameSpace = (function () {
// start with var to avoid global variables
var config = {
a : 'a',
b : 'b'
},
// continue with a comma to stay in the same var declaration.
init = function ( arg ) {
doStuff( arg );
},
x = '3',
doStuff = function ( arg ) {
// dostuff
};
// end with a semi colon.
return {
init:init
};
}());

I think that this has the advantage that when someone else continues with this code he can extend it easily, while not accidentally introducing globlal variables...

var nameSpace = (function () {
// start with var to avoid global variables
var config = {
a : 'a',
b : 'b'
},
// continue with a comma to stay in the same var declaration.
init = function ( arg ) {
doStuff( arg );
},
x = '3',
doStuff = function ( arg ) {
// doStuff
},// if you add a new var it's not global
doMoreStuff = function (){
// do more stuff
}
;
// end with a semi colon.
return {
init:init
};
}());

What do you think, is this a good way to structure your code or am I missing something?