Tuesday, July 23, 2013
Software Patents
I have always hated the patent system and think that it is abused and inhibits innovation. One of my favourite writers, Joel Spolsky has written a great blog on killing patents.
Monday, July 22, 2013
JavaScript: Length of Arrays
JavaScript arrays are dynamic and flexible with a few gotcha's. Today we will have an in-depth look at the length property, which is an unsigned 32 bit integer that specifies the number of elements in an array. Arrays have 0 based indices, therefore the length property will be the highest index + 1. Lets have a look at some examples
var a = [];
a.length //return 0, a is an empty array
var b = [2,4,6];
b.length //return 3
b[3] = 8;
b.length //returns 4
b['a'] = 5; //never a good idea to use an array as an associative array, instead use a plain object
b.length //returns 4, non numeric indices are ignored in the length property
b[10000] = 50;
b.length //returns 10001, the positions between 3 and 10000 are padded with undefined
Setting the length of an array will shorten it as well
b.length = 4
b.length //returns 4, shortened the array from 10001 to 4
The safest way to empty an array is to set the length to 0
b.length = 0; //empty's an array
Next, I will be looking at the slice and splice functions on an array, till next time.......
var a = [];
a.length //return 0, a is an empty array
var b = [2,4,6];
b.length //return 3
b[3] = 8;
b.length //returns 4
b['a'] = 5; //never a good idea to use an array as an associative array, instead use a plain object
b.length //returns 4, non numeric indices are ignored in the length property
b[10000] = 50;
b.length //returns 10001, the positions between 3 and 10000 are padded with undefined
Setting the length of an array will shorten it as well
b.length = 4
b.length //returns 4, shortened the array from 10001 to 4
The safest way to empty an array is to set the length to 0
b.length = 0; //empty's an array
Next, I will be looking at the slice and splice functions on an array, till next time.......
Tuesday, July 16, 2013
Friday, July 12, 2013
JavaScript: More on Arrays
So what else can you do with JavaScript Arrays? Arrays are like a swiss army knife, you can use it like an array, you can use it like an object, you can use it like a stack and like a collection, whatever suits your purposes. Arrays have the following functions available
- length - return the total number of elements in the array
- join - concatenates all the elements in the array into a single string separated by the parameter passed
- concat - concatenates an array to the array and returns a new array
- sort - sorts the array by character encoding by default or by a user defined function
- reverse - reverses the elements of the array
- slice - returns an array that is a subset of the original array
- splice - can insert, remove or replace one or more array elements
- push - adds an element to the end of an array
- pop - it removes and returns the last element of an array
- shift - it removes and returns the first element of the array shifting all other elements down
- unshift - inserts elements at the beginning of an array shifting all the other element up
- toString - converts all the elements to a string and concatenates them to a comma delimited string
- toLocaleString - converts all the elements to a localised string and concatenates them to a comma delimited string
Tuesday, July 02, 2013
JavaScript: Arrays
The JavaScript array construct is very flexible, and I especially love the fact that you can put anything into a JavaScript array. There is no type police breaking your code. Arrays in JavaScript are not true arrays, but an Object with special array like functionality. So how do we define an Array
var x = new Array (1, 5, "ten", "twenty");
or
var y = [1, 'a', true, 7.5, new Object()];
or
var z = [ [1, "hello"], [2, {name:"test"}]];
Array indexes are zero based and can be accessed as follows
var value = x[0] //value will be 1
var anotherValue = z[1][1]['name'] //anotherValue will be test
Array indexes are generally integers greater than or equal to 0 and less than 232 - 1, anything else would be converted to a string and looked up as a string.
Arrays can also act as a dictionary with ease, using the x variable defined above, you could do the following
x['a'] = 12345
Yip the power of arrays in Javascript is terrifying, but liberating. I will look at some other array functionality in the next instalment
var x = new Array (1, 5, "ten", "twenty");
or
var y = [1, 'a', true, 7.5, new Object()];
or
var z = [ [1, "hello"], [2, {name:"test"}]];
Array indexes are zero based and can be accessed as follows
var value = x[0] //value will be 1
var anotherValue = z[1][1]['name'] //anotherValue will be test
Array indexes are generally integers greater than or equal to 0 and less than 232 - 1, anything else would be converted to a string and looked up as a string.
Arrays can also act as a dictionary with ease, using the x variable defined above, you could do the following
x['a'] = 12345
Yip the power of arrays in Javascript is terrifying, but liberating. I will look at some other array functionality in the next instalment
Subscribe to:
Posts (Atom)