If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

String.prototype.test

The amount of times I thought test was a prototype of String instead of RegExp and written:

Code:

alert("hi there how are you".test(/hi/))

I can't even nearly count...

Code:

<script type="text/javascript">
String.prototype.test=function(reg){if("test" in reg)return reg.test(this);return null;}
alert("hi there how are you".test(/hi/))
alert(/hi/.test("hi there how are you"))
alert("hi there how are you".test(/bye/))
alert(/bye/.test("hi there how are you"))
</script>

Very nice one BigMoosie! Never thought of using the bitwise operator before in place of Math.floor nor using commas instead of semicolons to concentrate the while-loop body into a single statement to avoid curley brackets. Very nice code indeed!

The built in sort() function treats Number object as strings making "12" appear before "2".
This sort below is a selection sort that overcomes this problem for numbers however strings will still exhibit this annoying property.

The first line (this.sort()) is in place as it increases the speed by roughly a factor of 10, however it will still work without it and should probably be removed if you know your array will already be mostly sorted.

An Array Prototype to convert an array of 'words'
to a dimensioned array where each field is an array
with field 0 holding the original word
and field 1
the number of occurrences of characters in a string
passed to the prototype.

// All variable, function etc. names are prefixed with 'f68' to minimise conflicts with other JavaScripts

Variables declared either as a parameter or with the keyword "var" are local to that function and cannot conflict with other variables. Try accessing your variables from outside your function and you will notice it is impossible making it pointless to obscure the variable names.

In the occasional surcumstance that a persistant variable is required and has to be declared outisde of your function you can wrap both the function and the variable in an anonymous function like this:

If you have a javascript intense thread that you wish to execute on user events such as mouse move then sometimes it can make your code non-responsive as it may execute perhaps 50 times when the mouse is moved only a short distance, this here will solve the problem, just call myFunct.wait(params) and it will be alot more resource saving.