Jan 24

Meh;

var Dog = function() {

    this.say = function( name ) {

       return name;

    }

};

var Animal = function() {

    this.inherit = Dog;
    this.inherit();

};

var pet2 = new Animal();

pet2.say("bob");

Something about writing on a whiteboards.

Of course, there’s load of different ways you could write this, meh.

  • Share/Bookmark
May 21

I was discussing JavaScript 1.7 / 1.8′s specification this morning with one of my colleagues Chris Constandinou, he’s working on a XUL app for Tesco at the moment while I’m assigned to the redevelopment of www.tescodigital.com

He has the luxury of using some of the new parts of the specification within the standalone player that he’s working on, including array comprehensions.

I was interested in getting some performance tests done to compare the speed of >= JavaScript 1.7 ‘Array Comprehensions’ vs normal looping, so he wrote this:

	var a = new Array( 1000000 );
	var start1 = new Date();
	var a = [i * i for each ( i in a )];
	var end1 = new Date() - start1;
	console.log( '(AC vs while) array comprehension speed: ', end1 );

	var start2 = new Date();
	a = new Array( 1000000 )
	var i = -1, len = a.length;
	while ( i++ < len )
		a[i] = i*i;
	var end2 = new Date() - start2;
	console.log( '(AC vs while) while loop speed: ', end2 );

	var obj = {};
	[obj['key' + i] = obj['value' + i] for each ( i in new Array( 1000000 ) )];

	var start3 = new Date();
	var a = [k + ': ' + v for each ( [k,v] in Iterator( obj ) )];
	var end3 = new Date() - start3;
	console.log( '(AC vs for loop) array comprehension speed: ', end3 );

	var start4 = new Date();
	var b = [];
	for ( var k in obj )
		b.push( k + ': ' + obj[k] )
	var end4 = new Date() - start4;

	console.log( '(AC vs for loop) for in loop speed: ', end4 );

The results are interesting (lower number is better):

(AC vs while) array comprehension speed: 0
(AC vs while) while loop speed: 1202
(AC vs for loop) array comprehension speed: 0
(AC vs for loop) for in loop speed: 0

  • Share/Bookmark
Tagged with:
Feb 17

I had a somewhat confused face wondering whether I was just waiting for a slow connection to load one of the tile overlays on one side of the Google Map I was trying to implement in a page for the site I’m currently working on, until I stepped through the DOM / Script to notice a problem with GMaps overlays being out of sync due to the time they were taking to load.

This inevitably had the effect of making it impossible for the GMaps API to calculate the visible area until everything had initialised first, making the map controls think the overlay area was smaller than it really was.

It’s nice to see the inclusion of a checkResize method within the GMaps API, calling it through a simple setTimeout sorted the problem, however it would be nice if Google actually incorporated a live event to automagically fire the proceedures within the checkResize when the map container is resized, I guess it leaves things more open for us though, not all platforms will have resizable windows containing maps.

For the record I was using Cody Lindley’s ThickBox for the Lightbox

// Map code
var Horton = Horton || { };

Horton.GMap = function () {

return {

	init: function () {

		if ( GBrowserIsCompatible() ) {

			mapContainer = new GMap2( document.getElementById("map1") );
		        mapContainer.setCenter(	new GLatLng(37.4419, -122.1419), 13 );
			mapContainer.addControl( new GSmallMapControl() );
			mapGeocoder = new GClientGeocoder();

			Horton.GMap().showAddress("An Address");

		 }

	},

	showAddress: function (address) {				

		if (mapGeocoder) {

		        mapGeocoder.getLatLng( address, function(point) {

				if (!point) {

					alert("There seems to have been a problem");

				} else {

		            		mapContainer.setCenter(point, 13);
					var marker = new GMarker(point);
		            		mapContainer.addOverlay(marker);
		            		marker.openInfoWindowHtml(address);

				}
		        });

		}

		setTimeout('Horton.GMap().reloadContainer()', 10);

	},

	reloadContainer: function () {

		mapContainer.checkResize();

	}

};

};
  • Share/Bookmark
Tagged with:
Dec 15

Due to inconsistencies with IE’s handling of self-invoking recursive functions I’ve opted with arguments.callee for deep-cloning objects:

/**
@name cloneObject
@function
@description Deep clones an object / array

@param {Object} data Object to clone

@returns {Object}

@example
	var firstObj = { name: "Bob", secondNames: ["is","your","uncle"] };
	var clonedObj = cloneObject( firstObj );
*/
cloneObject: function( obj ) {
	var index, _index, tmp;

	if ( typeof obj.constructor !== 'object' ) {
		return obj;
	} else {
		if ( obj.concat ) {
			tmp = [ ];
			for ( index = 0, _index = obj.length; index < _index; index++ ) {
				tmp[index] = arguments.callee( obj[index] );
			}
		} else {
			tmp = { };
			for (index in obj) {
				tmp[index] = arguments.callee( obj[index] );
			}
		}
	return tmp;
	}

}

So far so good…

  • Share/Bookmark
Tagged with:
Dec 09

I’ve started playing around with a method for deep-cloning objects this morning and have got to here so far:

cloneObj: function cloneObj ( obj ) {

if ( obj.constructor == Array || obj[0] ) {

	var r = [ ], i;

	for ( i = 0; i &lt; obj.length; i++ ) {

		if ( typeof obj[i] === 'object' ) {

			r[i] = cloneObj( obj[i] );

		} else {

			r[i] = obj[i];
		}

	}

	return r;

} else {

	var r = { }, i;

	for ( i in obj ) {

		if ( typeof obj[i] === 'object' ) {

			r[i] = cloneObj( obj[i] );

		} else {

			r[i] = obj[i];

		}

	}

	return r;

	}

}
  • Share/Bookmark
Tagged with:
Oct 19

For all future posts, organising wordpress is a bit of a mission TBH.

  • Share/Bookmark
preload preload preload